pynput not seeing cap letters directly.
Using pynput to catch combo keys. Able to read 'h' but not 'H'. Python 3.6, windows 10 machine, tried running as admin. Able to see Key.shift and Key.ctrl_l but not the proper converted key. I hope I'm clear.
Perhaps my code below is missing something or it currently is not able to read combo-keys.
from pynput.keyboard import Key, Listener
def look_for_key(key):
letter = str(key)
letter = letter.replace("'", "")
if letter == 'Key.esc':
return False
print(letter)
with Listener(on_press = look_for_key) as l:
l.join()
From seeing other posts and examples, I should see it print out "H" if that is typed, instead I see "h".
You can detect if the key pressed equals Key.caps_lock and based on that, print or modify the key with the upper() method, but you have to transform the key to string.
Second step, create a global variable 'count' that will save the number of times the caps_lock key is pressed, in order to detect if the caps_lock was turned off. If the count variable is odd the program will print an uppercase key, otherwise, in lowercase.
from pynput.keyboard import Key, Listener
count = 0
def on_press(key):
global count
if key == Key.caps_lock:
count = count + 1
elif count%2!=0:
print(str(key).upper())
else:
print(str(key).lower())
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
Related
I was writing a simple python code for keylogger, it worked fine until I saved and closed the file. After I reopen the file, it is now giving me this error. I am not able to figure out the reason behind the error.
`
import pynput
from pynput.keyboard import Key,Listener
count = 0
keys = []
def write_file(keys):
with open("thefile.txt","a") as f:
for key in keys:
k = str(key).replace("'","")
if k == Key.space:
f.write(" ")
if k == Key.enter:
f.write("\n")
f.write(k)
def press(key):
global count,keys
keys.append(key) #this will update the keys list everytime we press the keys.
count += 1 #this will count the number of presses
print("{0} pressed".format(key)) #this will print every key being printed
if count >=5:
count = 0
write_file(keys)
keys = []
def release(key):
if key == Key.esc:
return False
with Listener(on_press= press, on_release= release) as listener:
listener. Join()
`
Your key listener doesn't consume input only catch it on the fly. So everything you type will be in the input buffer. As soon as you type the command line tool get the hand and consume everything you typed.
df is not a command that's what your system says.
I am using processing.py and I am using keyPressed and key to detect keyboard input in my program. However, this means that only the most recently pressed key will be detected. For example, if I hold down g, and press h while holding down g, the program will only detect the h. How do I detect whether or not g is still being pressed?
If you're trying to use the keyPressed and key variables, you'll only be able to get the most recent key.
But you can get this to work by using the keyPressed() and keyReleased() functions together to keep track of which keys are currently down (keyPressed has fired for the key, but keyReleased has not).
Here's a simple example:
gIsDown = False
def draw():
if gIsDown:
background(255)
else :
background(0)
def keyPressed():
global gIsDown
if key == 'g':
gIsDown = True
def keyReleased():
global gIsDown
if key == 'g':
gIsDown = False
If you need to keep track of more than one or two keys then you probably want to store the down states in an array so you don't have to create a bunch of individual variables for all the key states. You can use the keyCode as array indexes to make this easier.
I want to create a script that uses a time input from the user. At the moment I am using PyInquirer because it looks cool but I am not sure if it is capable of doing what I need.
The first prompt asks the user to choose Duration or Time like so:
If the user then chooses Time the next prompt looks as follows:
Here the input must only have the default format of HH:MM [AM/PM] where values can only be selected by using the up and down arrows or typed. The cursor must ignore and skip the ':'.
Therefore, the HH and MM may be typed or spinned using the up and down arrow keys and the AM/PM are spinned as well. If typing the HH and MM the cursor must automatically skip the colon.
Here is my code so far:
from PyInquirer import prompt, print_json
typeQ = [
{
'type' : 'list',
'name' : 'type',
'message' : 'Duration or Time based?',
'choices' : [
'Duration',
'Time'
]
}
]
time = [
{
'type' : 'input',
'name' : 'time',
'message' : 'Specify time:'
}
]
typeA = prompt(typeQ)
if (typeA == 'Duration'):
pass
else:
time = prompt(time)
This is an example how to use pythons curses library with arrows as key input in a while loop. The mytime() method is only accessed via the arrow key strokes. Some additional keys are also added such you can play with it. See "#" for inline comments.
Please post next time your example code and what you've tried fist which is behind the screens you show in the question. Its sailing a little blind without code. If included it would make life easier to anticipate on what structure you use. It also helps up-voting your question instead of people down-voting it due to so-called lack of effort.
Now you have what I've posted and possibly you have to retrofit it to work for your own code. It was a fun morning exercise. Enjoy.
import curses, sys, time, re
screen = curses.initscr()
curses.curs_set(0)
screen.keypad(True)
curses.noecho()
regex_time = re.compile('[0-9][0-9][:][0-9][0-9]') # time expression selection key.
def mytime():
screen.addstr("\nTime entry please + [enter]. Input format is [HH:mm].\n")
event = screen.getstr(5) # use (0, 0, 15) for location of input string top left in cmd window.
if regex_time.match(event.decode()):
screen.addstr("\n You entry = %s h.\n\n >> Add code to do something else here << \n\n" % event.decode())
screen.refresh()
time.sleep(2)
# do something else goes here.
else:
screen.addstr('\nDid not receive time\nTry again next round.\n')
screen.refresh()
time.sleep(2)
# do something else goes here.
while True:
try:
screen.addstr("Press a key\n")
curses.echo()
event = screen.getch() # get single character
screen.refresh()
if event == curses.KEY_LEFT:
screen.addstr("Left Arrow Key pressed\n")
screen.refresh()
time.sleep(0.5)
mytime()
elif event == curses.KEY_RIGHT:
screen.addstr("Right Arrow Key pressed\n")
screen.refresh()
time.sleep(2)
elif event == 81: # 'Q' for quite program.
curses.echo() # shows which char you pressed.
screen.addstr('\nThe Quite key "%s" was pressed\n' % (event))
screen.refresh()
time.sleep(3)
break
else:
# keystroke identification except for above and regular expression that are comform time annotation.
curses.echo() # shows which char you pressed.
screen.addstr('\nThe key "%s" was pressed\n' % (event))
time.sleep(2)
screen.refresh()
except Exception as err:
# continue
print (err)
# below code could replace the break in the quite elif statement.
screen.addstr("\nExit program.\n")
screen.refresh()
time.sleep(3)
curses.endwin()
I am trying to write an interactive command line program using input keys to print specific text to the terminal, and it has the following structure:
from pynput.keyboard import Listener, Key
def choix_case(tour='X'):
variable = 1
def goto_case(a):
print ('\033[u', end='') #Escape code to reset the cursor position
print ('\033[', a*2, 'B', sep='', end='') #Escape code to move [a] times to the right
def print_choice():
#Some escape codes to change the style of the next printed text:
blinking('on')
bg_color()
fg_color(0xd7,0x5f,0x5f)
print('X', end='') #The actual printed text
#Escape code to reset the style:
blinking('off')
fg_color()
bg_color()
goto_case(variable)
print_choice()
def on_press(key):
nonlocal variable
if key == Key.right:
variable+=1
if key == Key.enter:
return False
goto_case(variable)
print_choice()
with Listener(on_press = on_press) as clef:
clef.join()
It is supposed to execute the goto_case and print_choice functions each time a key is pressed, and then close the Listener when Enter key is pressed.
Basically, it prints an 'X', and adds another 'X' on the next position each time I press the Right key, but it prints nothing, and then prints them all at once (for example 'X X X' for 3 times) after I press enter.
The problem is that nothing is printed until the Enter key is pressed, and then everything is printed at once. It's as if the Listener saved all the times on_press() ran, and then released them once it closed.
And what is even weirder is that if I printed a '\n' at any point inside the on_press() function ( or inside goto_case or print_choice which are called inside on_press, or I remove the end='' option from any print), it prints what happens each time I press a key. So the problem seems to be in the fact that there is no newline char printed? Why would that block the printing while the Listener is still running?
I am trying to get the duration of a key pressed using python but my code works only for the first time and then the next key presses show the total time since the program executed.
from pynput import keyboard
import time
def keydet(key):
return False
def keypress():
def callb(key): # what to do on key-release
ti1 = time.time() - t
ti2 = str(ti1) # converting float value to string
ti3 = ti2[0:5] # cutting the seconds ( time ) , without it , it will print like 0.233446546
print("The key", key, "Pressed For", ti3)
kp = key
print(kp)
a = time.sleep(0.0001)
return key # stop detecting more key-releases
def callb1(key): # what to do on key-press
return False # stop detecting more key-presses
with keyboard.Listener(on_press=callb1) as listener1: # setting code for listening key-press
listener1.join()
t = 0
t = time.time()
with keyboard.Listener(on_release=callb) as listener: # setting code for listening key-release
listener.join()
#Keypress Detector
with keyboard.Listener(on_press=keydet) as listener:
keypress()
I tried googling it but couldn't find the best solution for me. I also saw some questions in this site but nothing worked for me.
I don't know exactly how to do this, but maybe I can point you in the right direction. Have you heard of keyboard make/break codes? When you hold a key down, it keeps sending data to your PC. If there's some low level way to see these, you could simply count them as a way of judging duration. Happy googling!