How can I detect the Windows key modifier for KeyEvent? I have add the code:
textField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if ((e.getKeyCode() & KeyEvent.VK_ESCAPE) == KeyEvent.VK_ESCAPE) {
textField.setText("");
}
}
});
But the problem is, when I use the Windows zoom and try to exit from it using Win + Escape, if focus is in TextField, its content clears. I've tried filter by e.getModifiersEx(), but it returns 0. The only way I've found is to detect whether Windows pressed or not, is to create boolean field and change it's value when Windows pressed/released.
So, is there any way to get the Windows key pressure state from KeyEvent for ESCAPE released event?
The way I used for myself:
AbstractAction escapeAction = AbstractAction() {
public void actionPerfomed(ActionEvent e) {
setText("");
}
}
textField.addCaretListener(new CaretListener() {
#Override
public void caretUpdate(CaretEvent e) {
if (textField.getText() == null || textField.getText().isEmpty()) {
textField.getActionMap().remove("escape");
textField.getInputMap().remove(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
} else {
textField.getActionMap().put("escape", escapeAction);
textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), escapeAction);
}
}
});
Related
So I searched Stackoverflow, but couldn't find any actual answer that I got. If there's already an answer to this question, please tell me.
I have a class with a showDescription method. This prints a string variable.
I require this method to be called whenever the "d" key is pressed, in the main method. So, what would the code be to implement the key press/down event?
Do this if you have a swing application:
f.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if ((e.getKeyCode() == KeyEvent.VK_D) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
System.out.println("woot!");
}
}
#Override
public void keyReleased(KeyEvent e) {
}
});
you can read more here and here
If you have a console application then use:
Read Input until control+d
The title might be a little misleading, didnt know how to put my problem short.
Basically what im doing is im using keyboardlistener to find out which keys are down and according to that im moving my game character.
The problem is, when you click out of the window, while holding down a key my listener doesnt register the keyReleased event.
I tried to fix it by using mouse listener and the mouseExited event, but that doesnt fix it all the time, sometimes it does sometimes it doesnt.
Heres my implementation:
Keyboard:
public void mouseLeftWindow()
{
for(int i =0;i<KEY_COUNT;i++)
{
keys[i] = false;
}
}
#Override
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode>=0 && keyCode<KEY_COUNT)
{
keys[keyCode] = true;
}
}
#Override
public void keyReleased(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode>=0 && keyCode<KEY_COUNT)
{
keys[keyCode] = false;
}
}
where keys[] is a boolean[] describing, which codes are pressed
mouse:
#Override
public void mouseExited(MouseEvent e)
{
mouseMoved(e);
keyboard.mouseLeftWindow();
}
Your program will listen for further key events even when your mouse exited the component. That means you set everything to false on exit but if a key is still pressed it will be set to true immediately again. I think you are looking for a FocusListener instead of a MouseListener.
addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
}
#Override
public void focusLost(FocusEvent e) {
keyboard.mouseLeftWindow();
}
});
I have made my own version of Tetris in Java and i have added the possibility of moving the shapes both with JButtons and with certain keyboard keys. The code snippet i used is the following:
leftButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
moveLeft();
}
});
rightButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
moveRight();
}
});
rotateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
rotateMovingShape();
}
});
myPanel.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == event.VK_A)
{
moveLeft();
}
if (keyCode == event.VK_D)
{
moveRight();
}
if (keyCode == event.VK_S)
{
rotateMovingShape();
}
}
});
The problem i have is that after i use the JButtons, i cannot longer control the shapes with the keyboard keys. I suspect it has something to do with gaining/losing focus, but i am not sure. Could anyone tell me what's going on?
You get that problem, because you use KeyListener, instead of that you need to use Key Bindings. For example:
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A,0), "aPressed");
component.getActionMap().put("aPressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("a key");
}
});
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D,0), "dPressed");
component.getActionMap().put("dPressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("d key");
}
});
// other bindings
where component is your JPanel.
A KeyListener only receives key events when the component has the keyboard focus. Clicking on the buttons transfers the focus to them and away from your panel, so you don't get the events. Any one of the following approaches will solve this:
Call setFocusable(false); on the buttons so that they won't steal the focus.
Add the KeyListener to the buttons too.
Use key bindings instead of a KeyListener, so that you can catch the key press whether the component has focus or not.
I am writing a game and I have just tried to add the KeyListener. I have experience with java including KeyListeners but I for some reason cannot figure out why this code only works some of the time.
Here is my listener function:
public void Listener() {
System.out.println("[INFO] Listener() Ran.");
KeyListener kl = new KeyListener() {
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()=='a'){
System.out.println("[DEBUG] A Pressed.");
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
};
panel.addKeyListener(kl);
System.out.println("[DEBUG] panel added KeyListener.");
}
This code works probably only 1 out of 10 times that I run it. Maybe even less. Any ideas on why this is?
The getKeyChar should be called in the keyTyped. The getKeyCode() == KeyEvent.VK_A in the other both methods.
I have the ListSelectionListener which tells me when the cell is selected with the mouse.
JGrid grid = new JGrid();
grid.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
grid.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(final ListSelectionEvent e) {
e.getFirstIndex();
e.getLastIndex()
}
}
I want to select the sell only when the button shift is hold. How can i do it?
I need it for the multiple selection. When the user holds shift and clicks the cells it gives me getFirstIndex() and getLastIndex().
Add a KeyListener similar to this to your JGrid, assuming JGrids takes keyListeners
boolean shiftIsDown = false;
yourJGrid.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode == e.VK_SHIFT) shiftIsDown = true;
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode == e.VK_SHIFT &&
shiftIsDown == true) shiftIsDown = false;
}
public void keyTyped(KeyEvent e)
{
// nothing
}
});
Now when you get a valueChanged() event, just check to see if your boolean "shiftIsDown" value is true, and if so you can do your selection.