Hack Forums
[Python] [solved]Pynput - capturing CTRL + C - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] [solved]Pynput - capturing CTRL + C (/showthread.php?tid=5683147)



[solved]Pynput - capturing CTRL + C - TheWarOwl - Jul 31, 2017

EDIT: The solution is to use key.name

So assuming I use the example code:

PHP Code

<?php 
from pynput
.keyboard import Key, Listener

def on_press
(key):
    print(
'{0} pressed'.format(
        
key))

def on_release(key):
    print(
'{0} release'.format(
        
key))
    if 
key == Key.esc:
        
# Stop listener
        
return False

# Collect events until released
with Listener(
        
on_press=on_press,
        
on_release=on_release) as listener:
    
listener.join() 

When you press CTRL + C and release it, the release function outputs:
'\x03' Which I believe is ascii for ctrl^c, which I could handle with a simple dictionary I believe.

The issue is the keypress function gets this character: 
Which I can do nothing with...

Anyone got any ideas on what I can do?

EDIT: The documentation shows "Controller.modifiers" but not much on how it's used.

I really hope this isn't a stupid question, I'm completely lost.


* - _Pr0xy - Jul 31, 2017

What I did when I created my keylogger was figure out what ascii code was printed when that combination was used, so I don't know how reliable it is, but it looked similar to this:
if key.char == "":
string += "[CTRL-C]"
This doesn't work for some combinations like CTRL-Z, but it does for CTRL-C and V

EDIT: The ascii code isn't showing up but it is meant to be that icon for \x03


* - TheWarOwl - Jul 31, 2017

(Jul 31, 2017 01:36 PM)_Pr0xy Wrote:  What I did when I created my keylogger was figure out what ascii code was printed when that combination was used, so I don't know how reliable it is, but it looked similar to this:
if key.char == "":
string += "[CTRL-C]"
This doesn't work for some combinations like CTRL-Z, but it does for CTRL-C and V

Yes, that'd work with the keyrelease function, but it doesn't work with the keydown function... because the results are sometimes the same.

Thank you though for the input 👍