Create an actionlistener(?) in Java - java

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

Related

JAVA Clipboard (copy,paste,cut) How To; Most Efficiently

Looking over lot of examples that are sometimes helpful and other times are not I would like to ask the community what is the most efficient way of creating the Clipboard that has cross application capabilities in the sense copy& paste from the browser to app. and vice versa. I was looking at some of the examples out there and it seems everyone kind of has their own way of doing it, just curious on the methods you guys had the most success with that is short and does whats needed, thanks.
Should this not work? userField is my JTextArea and my class is implementing MouseListener
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent mouseEvent) {
userField.addMouseListener(this);
int modifiers = mouseEvent.getModifiers();
if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
System.out.println("Left ");
}
if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
System.out.println("Middle ");
}
if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
System.out.println("Right button is being real naughty");
}
};

Detect the Windows key modifier

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);
}
}
});

Java - keyboard state after leaving window

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();
}
});

Default close shortcut does not work

The default close shortcut (Cmd+q on Mac) doesn't work on this program i coded in java, do you know why?
I am an absolute beginner and would be glad if you helped me!
The code:
public static void main(String[] args) throws IOException, AWTException{
final Robot robot = new Robot();
robot.delay(2000);
while(true)
{
{
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(50);
}
}
}
}
That would be because Java is trying to be as cross-platform (or platform-independent) as it can be. You could make your own closing shortcut, using the Key class explained in your previous questions (specifically: How to cast a keyboard event). However, I don't think you could detect mac-specific keys, unless you dive into JNI (Java Native Interface), but if you are a beginner I wouldn't recommend it just yet.
For example, say you would like the shortcut to be CTRL+Q. Add another field in your Key class:
private boolean ctrlPressed = false;
Then, make a pressing check:
#Override
public void keyPressed(KeyEvent e)
{
//Previous code
if(e.getKeyCode() == KeyEvent.VK_CONTROL)
{
ctrlPressed = true;
}
}
#Override
public void keyReleased(KeyEvent e)
{
//Previous code
if(e.getKeyCode() == KeyEvent.VK_CONTROL)
{
ctrlPressed = false;
}
}
And finally, the Q part:
#Override
public void keyTyped(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_Q && ctrlPressed)
System.exit(0);
}

KeyListener only sometimes works

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.

Categories

Resources