PythonScript - How to get status of Ctrl , Alt , Shift keys also CapsLock and Scroll lock
-
PythonScript - How to get status of Ctrl , Alt , Shift keys also CapsLock and Scroll lock
I can’t find the answer here, that’s strange. I’d like to alter the behavior of my PythonScript thus need to get the status of them (alt shift ctrl Capslock Scrollock)
Thanks in advance!
H.C. Chen
-
-
Here’s an example script which shows how I do it most-often:
# -*- coding: utf-8 -*- from __future__ import print_function from ctypes import (WinDLL) user32 = WinDLL('user32') gaks = user32.GetAsyncKeyState gks = user32.GetKeyState VK_SHIFT = 0x10 VK_CONTROL = 0x11 VK_MENU = VK_ALT = 0x12 VK_CAPITAL = 0x14 VK_SCROLL = 0x91 shift_down = (gaks(VK_SHIFT) & 0x8000) != 0 control_down = (gaks(VK_CONTROL) & 0x8000) != 0 alt_down = (gaks(VK_ALT) & 0x8000) != 0 capslock_down = (gks(VK_CAPITAL) & 0x0001) != 0 scrolllock_down = (gks(VK_SCROLL) & 0x0001) != 0 print('shift_down:', shift_down) print('control_down:', control_down) print('alt_down:', alt_down) print('capslock_down:', capslock_down) print('scrolllock_down:', scrolllock_down)
Note that these calls cannot be done in all contexts. For example if you run this script from the menus while holding the Ctrl key, it won’t run the script (and thus you won’t run the code that detects the Ctrl key state), it will open the script into the editor!
-
I guess “down” is a misnomer for caps lock and scroll lock. What it (down = true) means in this context is that the corresponding keyboard light is on and that the functionality is active, i.e. pressing the
a
key without Shift will generateA
(for caps lock). -
@alan-kilborn Ha, this worked. Thank you very much!