Pygame knowing when shift+other key is pressed

后端 未结 2 427
野性不改
野性不改 2021-01-22 05:21

I am trying to create a program similar to Microsoft word, only heavily simplified. I need to be able to know when shift+(any key) is pressed, and display the proper value for w

相关标签:
2条回答
  • 2021-01-22 06:00

    If you look at the events for 'a' and 'A' while both have the key 97, you can use either the unicode (if strictly printable characters) or mod attribute to tell the difference.

    <Event(2-KeyDown {'scancode': 0, 'window': None, 'key': 97, 'unicode': u'a', 'mod': 0})>

    <Event(2-KeyDown {'scancode': 0, 'window': None, 'key': 97, 'unicode': u'A', 'mod': 1})>

    Note: the mod is different for shift and caps lock characters event though the unicode will be the same.

    0 讨论(0)
  • 2021-01-22 06:10
    src = r"`1234567890-=qwertyuiop[]\asdfghjkl;\'zxcvbnm,./"
    dest = r'~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'
    ch = chr(event.key)
    pressed = py.key.get_pressed()
    if pressed[py.K_RSHIFT] or pressed[py.K_LSHIFT] and ch in src:
        ch = dest[src.index(ch)]
    
    0 讨论(0)
提交回复
热议问题