0

I am writing some code to copy data from an Excel file, but I cannot get it to work.

Any help would be greatly appreciated.

Code used below that did not work:

pyautogui.hotkey('ctrl', 'shift', 'end')

or

pyautogui.press('ctrl')
pyautogui.press('shift')
pyautogui.press('end')
pyautogui.release('ctrl')
pyautogui.release('shift')
pyautogui.release('end')

also

pyautogui.keyDown('ctrl')
pyautogui.keyDown('shift')
pyautogui.keyDown('end')
pyautogui.keyUp('ctrl')
pyautogui.keyUp('shift')
pyautogui.keyUp('end')
aschultz
  • 1,658
  • 3
  • 20
  • 30

3 Answers3

5

On Windows, you need the number lock off.

With the number lock on, pyautogui seems to choose "end" from the 1-numpad instead of the "end" key. But with the number lock off, it highlights to the end in Notepad or Notepad++.

This seems like an ambiguity pyautogui should be resolving, but it is a tricky case.

If you want to check if the number lock is on before sending pyautogui.press('numlock'), see this question: Python 3.x - Getting the state of caps-lock/num-lock/scroll-lock on Windows

aschultz
  • 1,658
  • 3
  • 20
  • 30
2

Below line worked fine

pyautogui.hotkey('ctrl','shiftright','shiftleft','end')
eficazz
  • 21
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – R. Marolahy Oct 14 '21 at 04:59
0

Use "ctypes library" to check the keyboard's numlock key state and either disable it or switch hotkeys accordingly.

Getting numlock key state code:

# ctypes accesses C functions through Python, interfacing with platform libs.
import ctypes

def is_numlock_on():

    """
    Use the GetKeyState function from the user32 DLL to check the state of the numlock key.

    A virtual-key code is a unique value assigned to each key by the operating system.
    The virtual-key code for numlock key is 0x90.
    """

    return True if ctypes.windll.user32.GetKeyState(0x90) & 1 else False

After this, you can press numlock key to disable it depending on its current state but I suggest you be independent of numlock.

So, instead, you should use conditional hotkeys based on the current state of numlock:

COND_HOTKEY = "ctrl, shiftright, shiftleft, end" if is_numlock_on() else "ctrl, shift, end"

pyautogui.hotkey(*COND_HOTKEY.split(", "))
Deepanshu
  • 890
  • 5
  • 18