I am trying to write some code to repetitively clear the console and output a message, whenever I press a key that is added to the console as if I am in an input() state, when backspace is pressed the last letter is deleted. I also want to add a "cursor" so that when pressing arrow keys the characters selected move left and right. Everything works as expected except for the arrow keys.
So I scoured the internet for any information on this. I found 1 post, 3 years ago. It is inactive and no answer was given. The post is here. It is very similar to my question but rather then detecting a press I need to stop random outputs coming out. I am using Python 3 with Windows.
This issue happens exclusively when using the Keyboard module and when detecting arrow presses. If I do not detect the arrow presses this does not happen even when importing the module. My code is as follows:
if keyboard.is_pressed("left"):
# at this point a "4" is outputted into the console and detected when it wasn't pressed
Similarly with up arrow "8" is outputted, with down arrow a "2" is outputted, and right outputs a "6". This obviously sounds ridiculous but the post I linked above (being the only occurence I have ever read about this) clearly describes this identical problem.
They only wanted to differentiate between a 4 being pressed and a left arrow being pressed. By using keyboard.hotkey or keyboard.on_press_key, both of which I tried, there were no differences in results.
This should be extremely easy to reproduce, but I would be interested to know if anybody was unable to reproduce this and if it is linked to a specific OS or version of Python. I experienced this same problem a few years ago and quickly gave up on the mini project, now I'm re-experiencing this problem, if there is another way that I can have a function run or detect if an arrow key is currently being pressed down without these numbers being outputted, that would be a solution to this.
Related
When I try to select all of the text in a file, I receive a notification that says:
(Ctrl + A) was pressed. Waiting for second key of chord...
Here is a screenshot of my "select all" keyboard shortcuts. How can I use Ctrl + A to select all text instead of getting a chord notification?
You have another key binding that begins with Ctrl+A, something like this:
A sequence of multiple keystrokes pressed one after another (rather than simultaneously) is called a "chord".
You might have accidentally created it, or might have recently installed an extension that adds the problematic binding. To find it, do like I have done in the screenshot and type "ctrl a" into the Keyboard Shortcuts search box (that tab can be opened by typing Ctrl+K then Ctrl+S). Then look for a binding that begins with Ctrl+A but has something else after it (in my case, another Ctrl+A, but it could be almost anything).
When you find the offending binding, right-click on it, and either remove or change it. Then Ctrl+A alone will resume working.
Note: This question and its answer pertain to Visual Studio Code, which is different from Visual Studio. See this question for information about the equivalent situation in Visual Studio.
You might be in a situation where you want to keep the chorded keybindings that are causing you the problem with your original keybinding. A potential workaround to this issue is turning your original keybinding into a chord as well. For example, you could having a 'stop' character that exits your chord. For example, "Ctrl+A ." where "." is your stop character.
So if for some reason you really liked "Ctrl+A" as the beginning keystroke for a bunch of chorded keybindings, then rebinding all current "Ctrl+A" keybindings to "Ctrl+A ." would free up that keystroke for any manner of other, chorded keybindings.
I was in a situation where Ctrl+x did not work and was considered the start of a keychord. Every time I pressed Ctrl+x, instead of cutting the selected text, the editor told me that it was waiting for the second key of the chord.
Looking at "Preferences->Keyboard shortcuts" did not help. Searching for Ctrl+X in the list showed that only the "Cut" command was mapped to this key.
I had to edit the file $HOME/.config/Code/User/keybindings.json (under Linux) and search for CTRL+x. It turned out another command was mapped to a keychord starting with this key combination. I am not sure know how I got in this situation.
In my case, reloading VS Code with extensions disabled fixed it, so I knew it was an extension. Took a while, but eventually figured out it was https://github.com/phsantos/nano-id-generator.
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 am getting an error with key bindings when I search within a VS Code (command F on Mac) notebook. When I hit enter the cursor remains in the search bar & text is highlighted. If I wanted to clear the search bar and hit 'delete' (backspace equiv), I get a chord message '(Backspace) was pressed. Waiting for second key of chord.."
However, if I look into keyboard shortcuts I find I actually do not have ones that begin with that key. Here is the screenshot of keyboard shortcuts with 'recording' turned on to get all that start with 'delete'
Anyone know what is going on here or how to fix this?
After pressing Ctrl+Shift+P, search for Toggle Keyboard Shortcuts Troubleshooting. This will log you the keyboard events. You may for example be pressing Backspace without Ctrl being up again, and this logs will show you the keybindings that caused this chord message.
After finding the keys that caused the chord, go to settings and find the other bindings.
If you can't find it, you may try going to a random command, double click it and press on the keys that you found as the cause of the chord. VS Code will tell you the other commands with the same key bindings, at the bottom of input pop-up. Click on it and find the other binding.
If these still don't work, an extension that you installed may cause this problem, but I don't know the solution in that case.
I would like to make a program for displaying some information and help (like it is sometimes done at libraries or in other public places). So basically some information screen. The problem is I would like to prevent the user from exiting the program and scrolling up, to see what others typed.
I would like to do that without importing big libraries like Pygame. There are plenty of modules for automatic mouse movement and and mouse clicks, but almost no modules for detecting it. If you know some please write them here. I am using Windows 10 and 7.
Try pynput. It's a bit fiddly to use when capturing, but can capture keyboard and mouse and also emit both.
This answer doesn't really answer the question asked in the title:
However it might answer the use case that you describe:
I don't have a windows computer at hand for testing:
but perhaps this could work:
import os
os.system('cls')
# now rewrite the lines, that a user is allowed to see.
Theoretically cls is supposed to clean the windows screen buffer and you wouldn't see any history and it should be impossible to scroll back.
I think most other solutions would quickly become complicated or could be circumvented by a user with some combinations of key presses and mouse movements.
However you had to explicitely rewrite whatever you want to keep on the display.
I am creating a Project Manager using wxPython it has a splitter window. On one side is a tree that shows the names of and opens the files and on the other size is a textctrl that is used to edit the file.
One problem I am having is that I would like it to go back 4 spaces when SHIFT and TAB are pressed, I have code working that add's 4 spaces when TAB is pressed.
I am also have a problem that when I add a file that is in a different folder to my programs cwd the tree adds a new node and the file appears under this node and I am struggling to get the tree to save to a file.
Also I would like to know how to add an icon to an item in the tree from an external png file.
I would appreciate any help that could be given with either of these problems.
To catch multiple keys, you either need to catch EVT_CHAR or use an accelerator table. The latter is easier while the former may give you more control. Here are a couple tutorials for you:
wxPython: Catching Key and Char Events
wxPython: Keyboard Shortcuts (Accelerators)
I don't know use WxPython and so don't have much idea about it. But in general what you can do is whenever a key is pressed, call a callback function and you could get the time when the key was pressed. save it somewhere. And when the next key is pressed, get the time. compare both times, if there's not much significant delay (you can decide the delay), it means that both the keys were pressed simultaneously (although they were not).