Detecting a NUMLOCK / CAPSLOCK / SCRLOCK keypress/keyup in Python

拟墨画扇 提交于 2019-12-10 21:08:32

问题


In a game I'm developing, I want to detect a NUMLOCK keypress (or keyup), like registering a "callback" function when it gets pressed.

I'm not asking to read its state in a given moment, I can do that already, nor I'm interested in changing its value. It's about being aware of the keypress when it happens so I don't have to poll its state every tenth of a sec or so.

The game uses curses, and currently a blocking getch(). Curses does not detect NUMLOCK keypresses, and I never expected it to (led-related keyboard don't "produce" any key), and I wonder if there is any way to do so that is better than than replacing the main curses loop with a non-blocking getch() and call a keyboard_leds() function to read current state.

For example: I could start a new thread after initializing curses, passing its stdscr screen as argument, and that thread would register register a callback function (say, special_keypress()) for the event of a NUMLOCK keypress. Thus, that function would only invoke keyboard_leds() when needed, and then update stdscr.

I'm not sure if this is possible, and I'm aware that I'll probably have to go down to some architecture-dependent stuff (kernel/X11, etc), so if a cross-platform solution is not possible, then I'm fine with a Linux-only one.


回答1:


OK, it's like using a steam-hammer to crack nuts (especially if you are creating a CLI rogue-like game), but Pyglet gets the at least NUMLOCK keypresses in OS X. I couldn't make it work with either CAPSLOCK or SCROLLLOCK though, but my system hotkeys are pretty much overriden in every way possible, so it may be just me.

You could try and test if it works better for you, and then look at how Pyglet detects these keys.

import pyglet

from pyglet.window import key


window = pyglet.window.Window()


@window.event
def on_key_press(symbol, modifiers):
    if symbol == key.NUMLOCK:
        print 'NumLock was pressed, yay!'
    elif symbol == key.CAPSLOCK:
        print 'CapsLock was pressed, yay!'
    elif symbol == key.SCROLLLOCK:
        print 'ScrollLock was pressed, yay!'


@window.event
def on_draw():
    window.clear()


if __name__ == '__main__':
    pyglet.app.run()


来源:https://stackoverflow.com/questions/28324372/detecting-a-numlock-capslock-scrlock-keypress-keyup-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!