Python get keyboard input without getting stuck - python

I'm making a python program that changes my wallpaper every hour, but i want to be able to also change the wallpaper when i press a certain button.
this is the code i've tried
while True:
key = ord(getch())
but the only bad part is that it gets stuck on that until i press something. Is there a better way to do this?

You may be able to achieve what you want by using https://pypi.python.org/pypi/pynput.
See also its docs on pythonhosted http://pythonhosted.org/pynput/, especially the section about monitoring the keyboard http://pythonhosted.org/pynput/keyboard.html#monitoring-the-keyboard.
The following is an example from the docs:
from pynput.keyboard import Key
from pynput.keyboard import 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()
It will print every key you press until you press ESC, after which it will terminate.
Note that there are some operating system specific things to consider, for example on OSX the process must run as root.

I somehow accidentally found this.
import msvcrt
if msvcrt.kbhit():
Key = ord(getch())
if Key ==96:
#Do something here
And that seems to work. I think msvcrt.kbhit() is waiting for an keypress. Key = ord(getch()) takes the keypress and if Key ==96: checks if its the right keypress

Related

Best way to control python in a loop with keyboard?

New to python and wanting to write an app for a Raspberry Pi, but wanting to develop it on my computer first without having to assemble all the buttons, so I was thinking just using a keyboard in replace of buttons would work. But I can't seem to find a good solution for looping a script and watching for keyboard input at the same time.
Specifically, I am trying to run a loop that checks the status of buttons. If any button goes on, it will turn on a shared device. If that button then goes off it will turn off the shared device after a certain time period unless any other button is on. However, if in the time period that the button goes off, but the device has not shut down, another button goes on, the device will not shut down.
I was thinking this would be easier to develop using a keyboard and the numbers 1-8 for the 8 buttons I would have connected to the Pi.
If I use
x = input()
The script stops and waits for an input, but I need it to continue to run in the background.
So I tried with with the keyboard module
import keyboard
import time
while True: #
try:
if keyboard.is_pressed('q'): #
print("you pressed q")
except:
print("no key")
currenttime = time.time()
print (f'the new time is {currenttime}' )
time.sleep(1)
But it only accepts the keyboard input sometimes. Like not while it is sleeping, which I'm using as an example of if the script was busy doing other things.
I found this code searching around
from pynput import keyboard
def on_press(key):
try:
print('alphanumeric key {0} pressed'.format(
key.char))
except AttributeError:
print('special key {0} pressed'.format(
key))
def on_release(key):
print('{0} released'.format(
key))
if key == keyboard.Key.esc:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
Aside from the security issues, it grabs the keyboard input but I don't know where to put my loop.
I also played around a bit with tkinter, but since I'm not trying to build a GUI, this didn't seem like the right path.
I'd recommend looking into event binding: https://www.geeksforgeeks.org/python-binding-function-in-tkinter/

Disable windows Key when in full screen [duplicate]

I have a Python module that listens for a key combination using pynput then, once it's pressed, it types a string into a text program.
It works great! Except...
In the example below, the user's key combo is set to shift + space. This makes a lot of sense and will likely be the most common chosen key command for Windows users running my program. The trouble is, while the shift key is held down it changes what pynput types. Instead of 01/20/2019, it will type )!/20/2019.
I need a way to disable the keyboard until pyautogui is finished typing the string. Thanks a lot for your help!!!
Bonus question: I can't seem to get a result when the key combination includes a ctrl key. Key.ctrl simply fails to ever trigger, whilst other keys work fine.
from pynput.keyboard import Key, Controller, Listener
import time
keyboard = Controller()
def insert(): # check line 1 of config file
keyboard.type('01/20/2019')
# The currently active modifiers
current = set()
def on_press(key):
if key in COMBINATION:
current.add(key)
if all(k in current for k in COMBINATION): # I don't know what this k in current for k shit is.
current.remove(key)
insert() # run insert
if key == Key.esc:
listener.stop()
def on_release(key):
try:
current.remove(key)
except KeyError:
pass
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
``
You can use pyautogui.keyUp("shift") before you type.
def insert():
f=open('tc.txt')
line=f.readlines()
insert.timestamp=(line[0])
time.sleep(.1)
pyautogui.keyUp("shift")
pyautogui.typewrite(insert.timestamp)

getting long press keyboard pressed listener in python

I want to make program so that until a key is pressed it does a specific event repeatedly and when it's released it don't do anything
I am not able to achieve the same, I used pynput and keyboard library but don't know how to get this done please help
As you mentioned you have to download pynput in order to use it.
on_press function works as you hold the any key
on_release key works when you release the key
from pynput.keyboard import Key, Listener
def on_press(key):
a=5
b=3
print(a+b)
def on_release(key):
if key == Key.esc:
return False
with Listener(on_press=on_press,on_release=on_release) as listener:
listener.join()

Detect keyboard press

I want to make a program that runs a code when you just click on a button on a keyboard for example, I press A and some code runs, but I do not have to press enter or input it to run it. Just like in video games, your character moves if you press W. Sorry if it is worded badly, I am pretty confused about this.
Keep in mind please it's Python 2.7
I am assuming you mean in the console and not in any gui like tkinter or something.
I'd suggest using pynput (pip install pynput)
with code similar to this
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
while True:
with Listener(on_press=on_press,on_release=on_release) as listener:
listener.join()
word of warning:
The above code also catches exit keys so ctrl + c will not stop the console. for that you would need to implement something to break out of the While loop when ctrl+c is pressed.

Programmed a keylistener, but it executes everything twice

I programmed a standard keylistener with pynput, but when using keyboard.type, it seems like the keys are released twice.
from pynput.keyboard import Controller, Listener
keyboard = Controller()
def on_release(key):
print('key {} released'.format(key))
if key.char == 'a':
keyboard.type('b')
with Listener(on_release=on_release) as listener:
listener.join()
# Pressing 'a' yields:
#
# key u'a' released
# key u'b' released
# key u'b' released
It doesn't seem like the key is pressed twice, only released twice. Is this the intended behavior? If not, what should be done to avoid this?
This is a bug in pynput.
As you have noticed, events can reach a Listener both from the system and when Controllers are invoked. The latter is true only for Xorg and win32, as they do no propagate synthetic events to listeners.
Or so I thought. It turns out that on win32, this is true only for mouse events; keyboard events appear to propagate normally. In any case, I have pushed a proposed solution here.
If you have the opportunity to test it, I would be grateful, otherwise I will merge it into master in a couple of days and make a new release. I only have access to win32 through VirtualBox, so I would appreciate some more bare-metal testing.

Categories