Amir Rahimpour
Amir Rahimpour

Reputation: 65

Python pyautogui Windows 10 control shift end combo fails

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')

Upvotes: 0

Views: 2081

Answers (3)

Deepanshu
Deepanshu

Reputation: 1120

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(", "))

Upvotes: 0

eficazz
eficazz

Reputation: 31

Below line worked fine

pyautogui.hotkey('ctrl','shiftright','shiftleft','end')

Upvotes: 3

aschultz
aschultz

Reputation: 1678

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

Upvotes: 7

Related Questions