Jump to content

Python keyboard module questions 2.0

Go to solution Solved by Sauron,
5 minutes ago, stefanmz said:

Yes but is there a keyboard command which if I press anything else on the keyboard,  goes to else and doesn't fulfill the if

you can use is_pressed to check if a given combination is currently being pressed. this will just instantly fall through when you run the program since it's not blocking. you can use read_event or read_key to block the program until something is pressed, then use is_pressed to check that what is being pressed is the "correct" combination. Note that as soon as you press anything the program will stop waiting and it will give you no time to press keys in succession; you might want to add a small delay after the read_key call so you have time to press all the keys.

import keyboard
import time

keyboard.read_key()	#block program until something is pressed
time.sleep(1)	#give user 1 second to press and hold all required keys
if keyboard.is_pressed('8+9'):	#ok if 8 and 9 are pressed at the same time
  print('ok')
else:
  print('not ok')

 

Hey! So I wrote some script and I used the keyboard module and I have such code:

keyboard.wait('key combination')
if keyboard.is_pressed('key combination'):
    do something 
    
else:print('whatever')

It does not work, if I don't press the key combination, it just does nothing and waits until I press that combination, but I want it to go to the else. If I press it, it does whatever it has to do in the if, but how do I go to the else? I want to make it if any other key except the key combination is pressed, go to the else. I tried keyboard record instead of wait but that didn't work either. I tried putting input() instead of keyboard.wait that didn't work either.  So what do I do?

Link to comment
https://linustechtips.com/topic/1450958-python-keyboard-module-questions-20/
Share on other sites

Link to post
Share on other sites

keyboard.wait is blocking your program until you press the keys... you can't proceed from there unless you press them, and if you do you'll fulfill the condition in the if statement.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

3 hours ago, Sauron said:

keyboard.wait is blocking your program until you press the keys... you can't proceed from there unless you press them, and if you do you'll fulfill the condition in the if statement.

Yes but is there a keyboard command which if I press anything else on the keyboard,  goes to else and doesn't fulfill the if

Link to post
Share on other sites

5 minutes ago, stefanmz said:

Yes but is there a keyboard command which if I press anything else on the keyboard,  goes to else and doesn't fulfill the if

you can use is_pressed to check if a given combination is currently being pressed. this will just instantly fall through when you run the program since it's not blocking. you can use read_event or read_key to block the program until something is pressed, then use is_pressed to check that what is being pressed is the "correct" combination. Note that as soon as you press anything the program will stop waiting and it will give you no time to press keys in succession; you might want to add a small delay after the read_key call so you have time to press all the keys.

import keyboard
import time

keyboard.read_key()	#block program until something is pressed
time.sleep(1)	#give user 1 second to press and hold all required keys
if keyboard.is_pressed('8+9'):	#ok if 8 and 9 are pressed at the same time
  print('ok')
else:
  print('not ok')

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

12 hours ago, Sauron said:

you can use is_pressed to check if a given combination is currently being pressed. this will just instantly fall through when you run the program since it's not blocking. you can use read_event or read_key to block the program until something is pressed, then use is_pressed to check that what is being pressed is the "correct" combination. Note that as soon as you press anything the program will stop waiting and it will give you no time to press keys in succession; you might want to add a small delay after the read_key call so you have time to press all the keys.

import keyboard
import time

keyboard.read_key()	#block program until something is pressed
time.sleep(1)	#give user 1 second to press and hold all required keys
if keyboard.is_pressed('8+9'):	#ok if 8 and 9 are pressed at the same time
  print('ok')
else:
  print('not ok')

 

Ok thanks! Well I will challenge myself to see if I can press them simultaneously without a delay and if I can’t I will add the time sleep. Thanks a lot!

Link to post
Share on other sites

On 8/23/2022 at 1:08 PM, Sauron said:

you can use is_pressed to check if a given combination is currently being pressed. this will just instantly fall through when you run the program since it's not blocking. you can use read_event or read_key to block the program until something is pressed, then use is_pressed to check that what is being pressed is the "correct" combination. Note that as soon as you press anything the program will stop waiting and it will give you no time to press keys in succession; you might want to add a small delay after the read_key call so you have time to press all the keys.

import keyboard
import time

keyboard.read_key()	#block program until something is pressed
time.sleep(1)	#give user 1 second to press and hold all required keys
if keyboard.is_pressed('8+9'):	#ok if 8 and 9 are pressed at the same time
  print('ok')
else:
  print('not ok')

 

well the time.sleep after read.key breaks the whole program and I don't know why. Even if I put a single key to be pressed(like command) where you don't need more time, when I press it while I have the time.sleep it goes to the else and fails the statement. Though remove the sleep and it works. However without the sleep, it will obviously work with only one key. If I want a key combination I need to use the sleep but with the sleep it fails the statement always even when I press the correct combination. So any thoughts? I did it like you said the syntax is the same.

Link to post
Share on other sites

Just now, stefanmz said:

well the time.sleep after read.key breaks the whole program I don't know why.

would probably help if you posted the program... the snippet as posted works just fine for me.

 

what is the purpose of this anyway? doesn't seem very useful to just wait for a key combination halfway through a program...

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

4 minutes ago, Sauron said:

would probably help if you posted the program... the snippet as posted works just fine for me.

 

what is the purpose of this anyway? doesn't seem very useful to just wait for a key combination halfway through a program...

oh no I am doing it just for fun. Maybe I'll just make it one key, I don't want to complicate stuff too much and edit other code. However do you know if I can add keys to keyboard. Because right now it doesn't recognize a lot of keys. Like § it doesn't recognize that. Or letters on the keyboard, it doesn't recognize those as well, just command space so on... How do I get it to recognize the rest?

Link to post
Share on other sites

55 minutes ago, stefanmz said:

oh no I am doing it just for fun. Maybe I'll just make it one key, I don't want to complicate stuff too much and edit other code. However do you know if I can add keys to keyboard. Because right now it doesn't recognize a lot of keys. Like § it doesn't recognize that. Or letters on the keyboard, it doesn't recognize those as well, just command space so on... How do I get it to recognize the rest?

where's the fun if I'm giving you the answers? 😛

 

if you're following the documentation and the keys aren't being recognized I don't know what to tell you, you should post an issue on the github page...

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

3 minutes ago, Sauron said:

where's the fun if I'm giving you the answers? 😛

 

if you're following the documentation and the keys aren't being recognized I don't know what to tell you, you should post an issue on the github page...

yeah that's why I didn't want to post it here, because I want to do it myself, I just needed a little clearance of the keyboard commands. Anyway, I decided to go with one key instead of a combination for it to work. However what's the command to clear the terminal? Do you know? Like to tell the program to clear the terminal?

Link to post
Share on other sites

On 8/28/2022 at 2:19 AM, stefanmz said:

yeah that's why I didn't want to post it here, because I want to do it myself, I just needed a little clearance of the keyboard commands. Anyway, I decided to go with one key instead of a combination for it to work. However what's the command to clear the terminal? Do you know? Like to tell the program to clear the terminal?

os.system('clear') that worked in the terminal so it's solved. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×