current position:Home>Key value of key combination in pynput in Python

Key value of key combination in pynput in Python

2022-05-15 05:35:53A small pit of goods

Monitor keyboard combinations

In the use of pynput When monitoring keyboard combinations , You need to know what the key value of the combination key is . For example, the following :
<48> It stands for ctrl+0 Key combination of .

from pynput.keyboard import Key, Controller,Listener
keyboard = Controller()
def on_press(key):
    print('{0}  Pressed '.format(key)) 


def on_release(key):
    print('{0}  Be released '.format(key))
    if key == Key.esc:
        return False
    if str(key) == r"<48>":# ctrl 0
        test()

def test():
	print (' Press down ctrl 0, Run the test program ')
#  Create a listener 
with Listener(on_press=on_press,on_release=on_release) as listener:
    listener.join()

More other key combinations

It's not all recorded , Just most of them . The follow-up will continue to supplement .


keyboard_key_dict = {
    
    r"'\x01'" : ['ctrl','a'],
    r"'\x02'" : ['ctrl','b'],
    r"'\x03'" : ['ctrl','c'],
    r"'\x04'" : ['ctrl','d'],
    r"'\x05'" : ['ctrl','e'],
    r"'\x06'" : ['ctrl','f'],
    r"'\x07'" : ['ctrl','g'],
    r"'\x08'" : ['ctrl','h'],
    r"'\t'"   : ['ctrl','i'],
    r"'\n'"   : ['ctrl','j'],
    r"'\x0b'" : ['ctrl','k'],
    r"'\x0c'" : ['ctrl','l'],
    r"'\r'"   : ['ctrl','m'],
    r"'\x0e'" : ['ctrl','n'],
    r"'\x0f'" : ['ctrl','o'],
    r"'\x10'" : ['ctrl','p'],
    r"'\x11'" : ['ctrl','q'],
    r"'\x12'" : ['ctrl','r'],
    r"'\x13'" : ['ctrl','s'],
    r"'\x14'" : ['ctrl','t'],
    r"'\x15'" : ['ctrl','u'],
    r"'\x16'" : ['ctrl','v'],
    r"'\x17'" : ['ctrl','w'],
    r"'\x18'" : ['ctrl','x'],
    r"'\x19'" : ['ctrl','y'],
    r"'\x1a'" : ['ctrl','z'],
    r"'\x1f'" : ['ctrl','shift','-'],
    r"<186>"  : ['ctrl',';'],
    r"<187>"  : ['ctrl','='],
    r"<189>"  : ['ctrl','-'],
    r"<192>"  : ['ctrl','`'],
    r"<222>"  : ['ctrl',r"'"],
    r"<48>"   : ['ctrl','0'],
    r"<49>"   : ['ctrl','1'],
    r"<50>"   : ['ctrl','2'],
    r"<51>"   : ['ctrl','3'],
    r"<52>"   : ['ctrl','4'],
    r"<53>"   : ['ctrl','5'],
    r"<54>"   : ['ctrl','6'],
    r"<55>"   : ['ctrl','7'],
    r"<56>"   : ['ctrl','8'],
    r"<57>"   : ['ctrl','9'],
    r"'~'"    : ['shift', '`'],
    r"'!'"    : ['shift', '1'],
    r"'@'"    : ['shift', '2'],
    r"'#'"    : ['shift', '3'],
    r"'$'"    : ['shift', '4'],
    r"'%'"    : ['shift', '5'],
    r"'^'"    : ['shift', '6'],
    r"'*'"    : ['shift', '7'],
    r"'('"    : ['shift', '8'],
    r"')'"    : ['shift', '9'],
    r"'_'"    : ['shift', '-'],
    r"'+'"    : ['shift', '='],
    r"':'"    : ['shift', ';'],
    r"'\"'"   : ['shift', "'"],
    r"'<'"    : ['shift', ","],
    r"'{'"    : ['shift', "["],
    r"'}'"    : ['shift', "]"],
    r"'|'"    : ['shift', "\\"],
    r"'?'"    : ['shift', "/"],
}

If you have any questions 、 Suggestion or error , You are welcome to comment, leave a message or send a private message to point out , Thank you very much !

copyright notice
author[A small pit of goods],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/135/202205142240049623.html

Random recommended