I am working on a pause key in my little school project, but for some reason it refuses to work. Using this code :
public void keyTyped(KeyEvent me) { //ESCAPE PLS WORK ...
code = me.getKeyCode();
System.out.println(code);
}
For some reason "code" always stays zero. I tried to put it in different voids(pressed/released etc), but it still does not work. What could be the reason?
Here's what the javadoc says about getKeyCode()
Returns: the integer code for an actual key on the keyboard. (For KEY_TYPED events, the keyCode is VK_UNDEFINED.)
And the value of VK_UNDEFINED is zero.
The javadoc also says:
public static final int KEY_TYPED
The "key typed" event. This event is generated when a character is entered. In the simplest case, it is produced by a single key press. Often, however, characters are produced by series of key presses, and the mapping from key pressed events to key typed events may be many-to-one or many-to-many.
So maybe you are looking at the wrong kind of key events. Maybe should be looking at the KEY_PRESSED or KEY_RELEASED events rather than the KEY_TYPED events.
Why not try the keyPressed() method again as in the example below:
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch( code ) {
case KeyEvent.VK_UP:
// handle up
System.out.println(code);
break;
case KeyEvent.VK_DOWN:
// handle down
break;
case KeyEvent.VK_LEFT:
// handle left
break;
case KeyEvent.VK_RIGHT :
// handle right
break;
}
}
Note that you must expect an integer.
Related
I am trying to create a sketch that will use the WASD keys to move a character around the screen in the Processing 3.3.7 IDE.
I am having an issue when one key is released and another is pressed at the same time, the key variable will not change to the new input for about a second. I cut down the offending code to this:
void draw(){
println(key);
}
With this code you can see, when you release one key and begin holding another in as close succession as possible (possibly only on the same frame) the printed value will not change for about a second (might differ depending on what your key repeat time). This is really annoying and I don't know how to get around it.
Thanks for reading!
The println() function is notoriously bad at timing, so I wouldn't trust it for micro-benchmarks like this.
Also, the key variable holds the last key that was interacted with, which could be the key you released instead of the key you pressed. For example, think about this sequence of events:
I press the a key. The key variable becomes a.
I press the s key. the key variable becomes s.
Now I release the a key. the key variable switches back to a.
I'm still holding the s key, so after a second, my keyboard triggers another s key event, switching the key variable to s.
If you want more fine-grained control over the keyboard events, I suggest using the event functions like keyPressed() and keyReleased(). You probably want to keep track of which keys are pressed using a set of variables that you set in the event functions. Here's an example:
boolean aPressed = false;
boolean sPressed = false;
void draw(){
if(aPressed){
// a is pressed
}
if(sPressed){
// s is pressed
}
}
void keyPressed(){
if(key == 'a'){
aPressed = true;
}
else if(key == 's'){
sPressed = true;
}
}
void keyReleased(){
if(key == 'a'){
aPressed = false;
}
else if(key == 's'){
sPressed = false;
}
}
Shameless self-promotion: here is a tutorial on handling input events. See the handling multiple key presses section to read more about this approach.
How to make small vk letters like case VK_k instead of VK_K?
using Switch Case
Thanks
public void onKeyPressed(KeyEvent e){
switch(e.getKeyCode()){
case VK_K:
moveDirection=1;
moveAmount = Double.POSITIVE_INFINITY;
break;
case VK_L: moveDirection=-1;
moveAmount = Double.POSITIVE_INFINITY;
break;
case VK_H: turnDirection=-1;
break;
case VK_J: turnDirection=1;
break;
case VK_SPACE: firePower = 1;
break;
}
}
You should read the description of KeyEvent fully - it contains the answer:
Virtual key codes are used to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes (such as "A", which comes from shift and "a").
Conclusion: Your keyboard has only one K key - hence there is only the constant VK_K. What other keys (shift, alt, ctrl, ..) you are pressing at the same time does not change the key code you get.
I'm developing a Java/Swing application, and I'm making my own handling for key events using a KeyListener on my JFrame.
My problem is, the key repeat feature of the OS is causing multiple keyPressed events to occur when I hold down a key, when I would like to only receive one.
One solution would be to keep the states of the keys in an array, and only accept the event if the state changes.
private boolean keysDown[] = new boolean[0x10000];
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (0 <= key && key <= 0xFFFF) {
if (keysDown[key]) return;
keysDown[key] = true;
}
// ...
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (0 <= key && key <= 0xFFFF) {
if (!keysDown[key]) return;
keysDown[key] = false;
}
// ...
}
This works, but is very clumsy, and while I only seem to find keycodes in the range 0 to 216-1, I'm not sure if ones outside that range can exist. (getKeyCode() returns int.) Another problem is that pressing down a key, releasing it while in another window, and pressing it again in my application would not register the event.
So, is there a better way to either
disable key repeat in my application or
detect repeated events reliably?
Replace your boolean array with HashSet<Integer> or TreeSet<Integer>. The key repeat is a function of the OS, so there is no way to disable it, only account for it.
How can I let my custom KeyListener listen for combinations of ALT (or CTRL for that matter) + more than one other key?
Assume I have 11 different actions I want the application to do, depending on a combination of keys pressed. ALT + 0 - ALT + 9 obviously don't pose any problems, whereas for ALT + 1 + 0 (or "ALT+10" as it could be described in a Help file or similar) I cannot find a good solution anywhere on the web (or in my head). I'm not convinced that this solution with a timer is the only possible way.
Thanks a million in advance for any suggestions!
Edit: Actions 0-9 + action 10 = 11 actions. Thanks #X-Zero.
You should not use KeyListener for this type of interaction. Instead use key bindings, which you can read about in the Java Tutorial. Then you can use the InputEvent mask to represent when the various modifier keys are depresed. For example:
// Component that you want listening to your key
JComponent component = ...;
component.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,
java.awt.event.InputEvent.CTRL_DOWN_MASK),
"actionMapKey");
component.getActionMap().put("actionMapKey",
someAction);
See the javadoc for KeyStroke for the different codes you can use while getting the KeyStroke. These modifiers can be OR'ed together to represent various combinations of keys. Such as
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,
java.awt.event.InputEvent.CTRL_DOWN_MASK
| java.awt.event.InputEvent.SHIFT_DOWN_MASK)
To represent when the Ctrl + Shift keys were depressed.
Edit: As has been pointed out, this does not answer you question but instead should just be taken as some good advice.
You can use KeyListener for this purpose by combining certain things. Look at the following example, which should be placed in an overridden keyPressed(KeyEvent e) method.
if (e.isControlDown() && e.getKeyChar() != 'a' && e.getKeyCode() == 65) {
System.out.println("Select All");
}
The string Select All will be displayed when Ctrl + a is pressed. The method e.isControlDown() checks whether the Ctrl key is pressed or not.
Similarly, the Alt key combinations can be done with the same method by using e.isAltDown() method.
The logic behind this is, e.getKeyChar() returns the character of the key presses & e.getKeyCode() returns its ASCII code. When Ctrl is pressed and hold, the e.getKeyChar() won't return a and e.getKeyCode() will be the same 65. Hope you understand this. Feel free to ask more.
ALT + 1 + 0 (or "ALT+10" as it could be described in a Help file or similar)
seems to clash with (from one of your comments):
So for example if the user wants to change data in column 11 (which would be called "10"), s/he'd press ALT + 1 + [lets go of both ALT and 1] 0.
Assuming that ALT+10 means 'Pressing ALT, pressing and releasing 1, pressing and releasing 0, releasing ALT' I propose trying this:
In keyPressed, listening for the ALT key, activate a boolean flag, isAltPressed, and create a buffer to hold key presses that occur (a string, say).
In keyTyped, if isAltPressed is active, append key codes to the buffer.
In keyReleased, listening for ALT again, open a conditional querying the buffer and executing actions.
public void keyPressed (KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_ALT){
buffer = ""; //declared globally
isAltPressed = true; } //declared globally
}
public void keyTyped (KeyEvent e){
if (isAltPressed)
buffer.append (e.getKeyChar());
}
public void keyReleased (KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_ALT){
isAltPressed = false;
if (buffer.equals (4948)) //for pressing "1" and then "0"
doAction();
else if (buffer.equals(...))
doOtherAction();
...
}//if alt
}
import java.awt.*;
import java.awt.event.*;
class KDemo
{
public static void main(String args[])
{
Frame f = new Frame();
f.setSize(500,500);
f.setVisible(true);
f.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
AWTKeyStroke ak = AWTKeyStroke.getAWTKeyStrokeForEvent(e);
if(ak.equals(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK)))
{
System.exit(0);
}
}
});
}
}
I would suggest that instead of using key combinations, consider some input field when the window isVisible() and is focused. The field can be hidden, like Windows' File explorer hidden filename search (enter a directory, type the filename and the correspondence is focused), or is visible, like in Ubuntu.
Key combinations are not designed for including more than one key other than modifiers, although you may be able to achieve this.
I think there's a simpler way which I am using. If the KeyEvent is ev then if you investigate:
(int)ev.getKeyChar()
you find that ctrl-a is 1, ctrl-b is 2 and so on. I wanted to use ctrl-s for save. So I just use:
(((int)ev.getKeyChar())==19)
to detect it. No idea why, but it works fine whereas:
ev.isControlDown() && ev.getKeyChar()=='s'
does not.
I've made a simple calculator using an tutorial online using Netbeans and it works fine when clicking the respective buttons, however I'm looking to improve it by allowing keypresses to work.
What I'd like is for the numbers 0-9 to work, +, -, *, / and enter as =.
I think I know how to do it, but can't seem to figure it out.
For example, the code for my 1 button is:
private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {
String btnOneText = txtDisplay.getText() + btnOne.getText();
txtDisplay.setText(btnOneText);
}
So for the keypress I created a keypress event but I'm not sure what the code is. I assume it's something like this:
private void jPanel1KeyPressed(java.awt.event.KeyEvent evt) {
//if statement to check if 1 key has been pressed, then execute rest of code
String btnOneText = txtDisplay.getText() + btnOne.getText();
txtDisplay.setText(btnOneText);
}
However I'm probably completely wrong. Any help?
You can use either of KeyListener or KeyBindings depending on your usage.
Since you are designing a simple calculator, there is no harm with KeyListener since there are not many controls.
Demo code for the same is.
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch( keyCode ) {
case KeyEvent.VK_0:
//handle 0 press
break;
case KeyEvent.VK_1:
// handle 1 press
break;
case KeyEvent.VK_2:
// handle 2 press
break;
case KeyEvent.VK_3 :
// handle 3 press
break;
//
}
}
You can find the keycodes here : http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html