pynput icon indicating copy to clipboard operation
pynput copied to clipboard

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

Open thecowmilk opened this issue 3 years ago • 4 comments
trafficstars

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?

thecowmilk avatar Dec 10 '21 17:12 thecowmilk

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

BenAufGitHub avatar Feb 19 '22 19:02 BenAufGitHub

You could try catching ctrl + c with this

try:
    # your code

except KeyboardInterrupt:
    # if ctrl + c

Apacelus avatar Feb 20 '22 00:02 Apacelus

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

jjwspring avatar Feb 21 '22 10:02 jjwspring

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.

BenAufGitHub avatar Feb 21 '22 14:02 BenAufGitHub