moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.79k stars 248 forks source link

Can't catch CTRL_L + C using pynput!!! #429

Open thecowmilk opened 2 years ago

thecowmilk commented 2 years ago

I have this pynput code:

import sys
from pynput import keyboard
from pynput.keyboard import Listener, Key

filename = "filetxt"
ctrlc = chr(ord("C")-64)
web = str(ctrlc)

def on_press(key):

    f = open(filename, 'a')
    if hasattr(key, 'char'):
        print(key)
        f.write(key.char)
    elif key == Key.space:
        f.write(" ")
    elif key == Key.enter:
        f.write("\n [Enter pressed] \n")
    elif key == Key.tab:
        f.write("\t")
    elif key == web:
        sys.exit(0)
    else:
        f.write('[' + key.name + ']')

    f.close()

with Listener(on_press=on_press) as listener:
    listener.join()

and it fails to catch ctrl + c I saw a stackoverflow answer that pynput printed the ^C like \x03 so I tried to reverse it and compare but it didn't work. Can someone show me how to do this?

BenAufGitHub commented 2 years ago

I was having the same problems a few hours ago and gathered some infos about how unicode works. The following methods should help you convert those unicodes from strg+... entries into actual chars from the alphabet. It is based on the concept that there is an order(int) command that gives you the unicode order, which for the alphabet, is between 1 and 26:

UnicodeReverse.txt

Apacelus commented 2 years ago

You could try catching ctrl + c with this

try:
    # your code

except KeyboardInterrupt:
    # if ctrl + c
jjwspring commented 2 years ago

I was having the same problems a few hours ago and gathered some infos about how unicode works. The following methods should help you convert those unicodes from strg+... entries into actual chars from the alphabet. It is based on the concept that there is an order(int) command that gives you the unicode order, which for the alphabet, is between 1 and 26:

UnicodeReverse.txt

Thank you for this! I had the same problem, and like your solution. I think there might be a bug in your .txt file. the: 0 < order < 26 checks means the code can't catch the z key, I think it should be: 0 < order <= 26

BenAufGitHub commented 2 years ago

I think there might be a bug in your .txt file. the: 0 < order < 26 checks means the code can't catch the z key, I think it should be: 0 < order <= 26

Glad I could help, and you're right, thanks for pointing out the bug.