Numpad enter key doesn't work in Python program - python

I have a Python program running on Raspberry Pi 3 (Raspbian version 10) and I need to do all operations only with Numpad. Everything works fine, but NUMPAD ENTER KEY doesn't work. When I want to enter my input via numpad enter, nothing happens. Does someone know how to fix it?
I tried this but doesn't work
number.bind("<KP_Enter>",keyTray)

Try binding <Return> instead of <KP_Enter>:
number.bind("<Return>", keyTray)

Related

Does msvcrt not work with python GUI libraries like tkinter? Or is is the IDLE? New to python GUI

I am using the latest version of PyCharm and Python. I'm trying to get my program to wait for specific keypresses like the arrow keys. I've been messing with the mscvrt (not worried about only windows) and using the getch() but it freezes my program everytime. I was able to get it to work printing to the console by using "emulate terminal in output console". But once I tried implementing it to a GUI with my code in tkinter it still freezes. Was wondering if it just doesn't work in GUI's and that's the issue (saw this mentioned on the internet) and if anyone had suggestions for what I could do.
For example:
This runs in the console and works. The arrow keys print as (b'\xe0' b'K') (left) and (b'\xe0' b'M') (right)
while True:
if msvcrt.kbhit():
key = str(msvcrt.getch())
if key == "b'w'":
print(key)
elif key == 'w':
print("suck it")
This freezes my program before I get the chance to press any keys. It seems to be only when I introduce GUI into the program. I also tried binding this as a function to space and it will freeze the GUI when I press space and crash after a bit. I also tried not using a while function and also without the kbhit part as well.
while True:
if msvcrt.kbhit()
user_input = str(msvcrt.getch())
start = tk.Label(window, text=(user_input), font=("arial", '25'))
start.place(relx=.5, rely=.7, anchor="center")
Appreciate any help/advice. I'm somewhat new to python and coding so not sure what the issue could be.

Python closes immediately after running script

FIXED IT BUT WONT LET ME DELETE
As described by the title, I am trying to run a pretty long script that closes immediately. It closes immediately without showing me the output.
Here is what I've tried:
I've attempted adding input('Press ENTER to exit') and it didn't work. I also tried doing that same command but replacing exit with close and putting exit () under it, as well as exit(0)).
I've also attempted opening it with python myfile.py and nothing. I've tried almost everything I could find.
I'm on Windows, I'm running the script directly from the file, I downloaded it and attempted to run (Sorry if this isn't clear I'm new to Python). The script is long so I don't know if it'd be useful putting it here, also it's kind of private stuff.
quit_control=input("Do you know to quit? press 'Y' and enter for quit.")
while quit_control!="Y":
quit_control=input("Do you know to quit? press 'Y' and enter for quit.")

How can I press the fn+f keys with Pynput?

I need to be able to press the Function key along with one of the f-keys with pynput, but it doesn't seem to recognize the function key as a keypress, nor does it recognize anything as a keypress when I hit both the function and one of the f-keys.
I'm trying to make a program that will change my keyboard backlight, and to do that I need to be able to have the program press fn+f3 and fn+f4, but I can't seem to make that work. Can anyone tell me how I can do this?

Python Console: Detect Ctrl X Key Press Event

I want to detect if Ctrl + X is clicked so that I can exit my Python application. I don't know how to do it? Please help.
Have you thought about using a function like the ones discussed here? Python read a single character from the user
Or maybe you could use curses.
Either way you would just need to find the key code for ctrl-X, it's 24.
The simple and the best option is to use the module keyboard. Install it using pip install keyboard.
Use the following code at the start of your code:
import keyboard as k
k.add_hotkey("ctrl+x",lambda: quit())
#Your code....
Well, it works easily, but, it will read keys from the whole windows. For example the program is running and you are using notepad currently and you pressed ctrl+x , then the python program will close too.

Python simulating 'enter' keypress in a game

So I've been attempting to get a script to type out a string of text in a video game (Guild Wars 2). Mainly I'm using pyautogui and for the most part it works fine. My issue is it seems I can't get the game to recognize 'enter'. For example if I have the code:
import pyautogui, time
time.sleep(2) #to allow me to have time to switch windows
pyautogui.press('enter')
pyautogui.typewrite("This is a test")
pyautogui.press('enter')
The two "press enter" function will not open and submit the text. If however I manually hit enter, the 3rd line types things out just fine.
I've also tried replacing press('enter') with keyDown followed by keyUp, but with still no results.
I've managed to create a workaround by having python hit F10, and then a separate Autohotkey script hitting enter when F10 is hit, but that is far from ideal. Are there any suggestions?
Extra note from comments: The script by itself works fine in other programs such as notepad. It seems to fail exclusively for the game client.
For anyone finding their way to this post. The answer is essentially that this can't be done in this fashion due to how most games interpret keypresses. A working system is shown here: Simulate Python keypresses for controlling a game
None of \n nor return works to me, so I have to use pydirectinput: https://github.com/learncodebygaming/pydirectinput
Install it using pip install pydirectinput, then import it and use as the README. Note that the file also needs to run as admin.

Categories

Resources