According to the JavaDoc for KeyEvent's getText() method, it returns
A String describing the key code, such as "HOME", "F1" or "A", for key
pressed and key released events. For key typed events, text is always the empty string.
However, when I try printing getText(), it works as expected for printable characters, but not for other keys. "a" and "A" are the results for pressing the a and Shift-A keys, but pressing the Home, F1, or arrow keys results in "". Similarly, it registers the pressing of the Shift key before the A key when I press Shift-A, but the getText() for Shift is "".
Strangely enough, pressing Ctrl + some key produces a non-printable control key.
Is this a bug, or at least a mismatch between the docs and the real behavior? I am using ScalaFX, so I suppose there could be a problem in the Scala interface, but I'm actually using the javafx.scene.input.KeyEvent and javafx.event.EventHandler classes, not their Scala wrappers, so I doubt that's the problem.
Related
I forgot the key combination for this functionality and I tried to search a bit unfortunately to no avail.
I think it was actually a super simple combination of control or shift with a letter key. The next input in the editor will show the received key.
In command-line mode and insert mode, Ctrl-V is used to insert the next non-numeric keypress literally (i.e. not as a Vim movement or mapping, for example), or to enter a character by its numerical value. E.g.
Ctrl-VEnter will input a CR character (which Vim typically displays as ^M)
Ctrl-VTab will input a Tab character, even if the expandtabs option is set
Ctrl-VRight will input the text <Right>, since there is no one character associated with the right arrow key
Ctrl-V065 will input A, whose ASCII value is 65
Ctrl-VU1F4A9 will insert a poop emoji after you have typed a non-hex-digit
More info at :help i_CTRL-V, :help i_CTRL-V_digit, and equivalent command-line mode topic :help c_CTRL-V.
I am getting a weird behavior while using pyautogui hotkey; where the special key used at times does "stick" and cause the subsequent text printed with pyautogui.typewrite() function to be printed incorrectly.
For example, this statement ran on OSX will cause shift to stick at random; causing the output to be printed capitalized.
pyautogui.hotkey("command", "shift", "f")
time.sleep(3)
pyautogui.typewrite("reference_file2.txt")
This will open the "recents" window in Finder, wait 3 seconds then will type a filename which should (or should not be) in the recents window. Sometimes the output of typewrite is printed like if Shift key was pressed.
So instead of
reference_file2.txt
You will see
REFERENCE_FILE#>TXT
which is exactly what you get if you type the original string with Shift key pressed.
Is this a bug in the hotkey function of pyautogui? Or am I supposed to do something to ensure that the keys pressed with hotkey is released, before moving on with the next statement? The documentation of pyautogui specify that the hotkey function does equal to a keypress and keyrelease sequence, so no action should be required, right?
In Atom cmd+| maps to toggling the file tree, how can I make sublime text do the same when these keys are pressed
If by toggling the file tree you mean hiding and showing the side bar, what you want to do is remap the key bound to the command that does that by default.
Note: Based on your use of cmd in the question, I'm assuming you're on a Mac so the answer is tailored for that. The operation remains the same but the key name will be different for the other platforms.
If you look at the default key bindings (available from the Preferences menu, which under MacOS is part of the Sublime Text menu entry) you can determine what keys are bound to what by default. In this case the command you want is toggle_side_bar; if you don't know the command you can find it by searching the key map to find the keys that you know you press to do this normally.
With that information, the default key binding for MacOS is:
{ "keys": ["super+k", "super+b"], "command": "toggle_side_bar" },
In your own custom key bindings file (available from the same menu as the defaults) you just need to insert your own key binding. For your case, that would look like this:
{ "keys": ["super+\\"], "command": "toggle_side_bar" },
You might think that the key should be super+| since that is what you want to bind to ultimately; however that will not work because in order to generate that character, you need to be pressing Shift, and thus without shift as a part of the key binding, it won't work.
Instead we use the character that is generated if you press the key without shift, which on my keyboard is \. That is a special character in JSON strings, so it needs to be doubled in order to be correct.
For keyboard layouts different from US QWERTY, you might need to swap that character for something else. You can determine the binding you want by opening the Sublime console with View > Show Console, entering the command sublime.log_input (True) and then pressing the key combination in question; Sublime will tell you what it thinks you pressed.
I know that there exists String.upper for the alphabet, but I'm looking for the shift version rather than the alphabetic version, such as the "uppercase" of the semicolon would be the colon. When I attempt to google I get irrelevant results about string.upper.
I think the best way to handle this would be a lookup table, for example:
local with_shift = {
["1"] = "!",
["2"] = "#",
["3"] = "#",
}
I haven't been able to find an existing table of this, but I'm sure one exists somewhere.
If you are using that for input, you can always check that both the ',' key and the shift key are pressed at the same time - and then execute whatever code you wanted to do.
As others are saying, keyboard layouts are not universal. In a French keyboard, for example, you have to press shift in order to get a '1' when pressing the '1' key in the "top" number row (it defaults to symbols). Neither LÖVE nor Lua know all the keyboard layouts available, and even if they did you could be using a custom-made one (I in fact use one on my computer).
Does Linux buffer keys typed in terminal so that you can read them later one key at a time?
I am asking because I want to catch ESC and arrow key presses and can't find a way to reliably read the codes. I put terminal in non-canonical mode and want program to block if there is no input, but if there is, I want to fetch only one key press for processing.
Update 2: Arrow keys is just an example. I need to identify key presses even for the keys with unknown escape sequences for my program.
There are two conflicting cases:
read(1) returns one character. For both function keys and ESC key this character will be 0x1b. To check if it was an arrow key, you need to read(1), which will block if only single ESC is pressed.
Solution: blocked read(1), non-blocked read(1)
Problem: if second read didn't match any function key, it may mean that it was buffered ESC followed by some sequence, or an unknown function key. How to detect unknown function key press?
read(4) returns at most 4 characters, but if you press ESC four times to let it buffer, you'll get a string of four 0x1b. The same problem to find out if there is an unknown function key press as above.
Can anybody explain how to deal with these problems in Linux terminal, or at least post a proof that Linux just doesn't have input buffer for keys?
You should read up on VT100 escape sequences.
You've discovered that the character code for the escape button (which is sent as a real character, but tends to be used almost exclusively for signalling the beginning of an escape sequence) is 0x1b.
To move the cursor UP: <ESC>[{COUNT}A
To move the cursor DOWN: <ESC>[{COUNT}B
To move the cursor RIGHT: <ESC>[{COUNT}C
To move the cursor LEFT: <ESC>[{COUNT}D
You can test these yourself by typing them into the terminal. Just type the keys one after another. My terminal does not recognize the count argument, but will work successfully if I type <ESC>[X (for X in A,B,C,D).
If your terminal isn't in VT100 mode, look up the escape sequences for whichever mode it's in. You might realize that depending too much on terminal specific escape codes restricts your program to one specific terminal type.