Python keyboard hotkey with toggling of the Scroll Lock

47 views Asked by At

pre: I'm not familiar with the low level win32Api calls.
I was testing a python script that binds a key to call a function and to toggle a Scroll Lock as an indicator that the key-remapping is ON with the keyboard module.

Here is an example that it works for the Caps Lock:

import ctypes
import keyboard

dll = ctypes.WinDLL('User32.dll')

CAPS_LOCK = 0X14
def toggle(event):
    print(f'Caps Lock: {bool(dll.GetKeyState(CAPS_LOCK))}')
    keyboard.send('Caps Lock')


keyboard.on_release_key('F12', toggle, suppress=False)
keyboard.wait()

The output is:

Caps Lock: False
Caps Lock: True

If I debug the module, I can see that it runs it as:

_winkeyboard.py
user32 = ctypes.WinDLL('user32', use_last_error = True)
user32.keybd_event(vk, code, event_type, 0)

the values are:
user32.keybd_event(20, 58, 0, 0)

So what's with the Scroll Lock? It does not turn it on or off.

keyboard.send('Scroll Lock')
...
user32.keybd_event(3, 57414, 0, 0)

It seems that the library does something wrong or I'm calling the wrong function.

0

There are 0 answers