I'm developing translation software for Linux using Python. I'm looking for a way to get the key pressed event. When an english letter key is pressed, I wan the pressed key to be assigned to a variable.
I've tried googling and reading various articles, but haven't had any luck so far.
Using Pygame, you care about the Pygame.KEYDOWN keyboard event. You can see how to use it in the pygame example aliens.py.
Related
Thank you for reading this.
So e.g I start csgo, minecraft, or whatever game. When I'm in game I want to open the chat by pressing the key "y", nevertheless, I've tried using pyautogui, keyboard, win32api and win32con, they only simulate virtual key presses and I guess that's the problem since the games don't accept it as real key presses.
Is there any way I could simulate it as actual key presses?
I appreciate any answer!
Have a good day :)
I don't think Python is the right tool for this, try looking for key mappers (like KeyMapper), they simulate keypresses better afaik and you can easily bound 'y' to press whatever button opens chat in the game you want.
I'm making a key press overlay for rhythm games. How would I go about obtaining keyboard inputs outside of the Python window's focus?
I would assume you would use the keyboard module, but I can't find any documentation on it other than how to have it record and simulate key presses. Upon importing it and looking at possible functions, I see that there are some that appear to have the functionality of getting keys down/up. I have no idea how to use them though. I'm not sure if I'm even on the right track with the keyboard module but maybe one of you guys know.
I'm working with wxPython, the Python bridge to wxWidgets, so I guess a wxWidgets user could reply. I'm playing with the KeyEvent class and since I'm testing my code on other platforms and other keyboards, I've made an incredible (to me) discovery: other keyboard layouts don't seem to be very well supported.
Here's what I mean: if you run the demo (KeyEvents.py in my case) and press on random letters, with a QWERTY keyboard, everything works. Switch to another layout, things still work... somewhat. Right now I have an AZERTY keyboard mostly used in France, so when I press the a key (which is on the English position of the q) a 'a' is reported. So far so good. But if I press a é (a key which is on the English 2 key), a 2 is reported. Reading the documentation didn't exactly help me to figure out what is going on. Is that a kind of mistake no one has noticed since wx is out? I would guess and hope not, but better late than never I guess.
To be more technical still, the KeyDown and KeyUp events have this problem. I have an AZERTY kleyboard, I press on the 2 key, and a 2 is reported, whereas a é is written on screen. Admittedly, the Char event does report a é, but, if I understood correctly, a Char event is not triggered in any context a KeyDown event is triggered. Perhaps I missed something here and perhaps that's the solution for me and international users.
Thanks in advance for your reply,
Char event is not triggered in any context a KeyDown event is
triggered.
False.
Due to each country has its own keyboard layout, wxWidgets sends two events when a key is pressed: One (key event) is the somewhat hardware code for that key; the other (char event) is the "translated" code, normally a Unicode point, but an ASCII code if you disabled Unicode support.
Keyevent is useful if you just want to do something on key-down or key-up events. If working with chars, then use only char event.
I am using Tkinter in python 3 to make a primitive game (I am aware of pyGame). My function that I have bound to KeyReleased is executed when any key is pressed. It seems to work just as KeyPress. Code down below
master.bind("KeyRelease",keyReleased) (with <> on the sides of KeyRelease
Most keyboards and OS's will send a steady stream of press/release events when you hold a key down. If you bind to <KeyRelease>, it absolutely will only fire on key release but your app may be betting multiple key release events, making it appear that they are happening on a press.
Is there any way to detect which keys are currently pressed using Tkinter? I don't want to have to use extra libraries if possible. I can already detect when keys are pressed, but I want to be able to check at any time what keys are pressed down at the moment.
I think you need to keep track of events about keys getting pressed and released (maintaining your own set of "currently pressed" keys) -- I believe Tk doesn't keep track of that for you (and Tkinter really adds little on top of Tk, it's mostly a direct interface to it).