In java how do I check if a user is pressing a certain key?

loserlobster

I am trying to make a hotkey program that runs in the background, and I can not figure out how to make it check when a certain key is pressed (In this case the minus key). I need the program to wait for a key to be pressed and then the program checks if that key is the minus key.

An example of my program as is now in pseudo code is somewhere along these lines.

while(true)
    {
        int pressedKey = userInputedKey;
        if(pressedKey == KeyEvent.VK_MINUS)
        {
            //action to be executed
        }
    }

I have found a lot of suggestions on other threads to use KeyListener but I might not be using it properly, so if your response is to use KeyListener please explain how to use it thoroughly (to be Exact I don't understand how when a KeyEvent is instantiated it already has the input of whatever key was pressed as the keyCode in that KeyEvent, so if I use it in the call to the KeyListener I will not get the proper results). Granted I do not know much about KeyEvent or KeyListener, so I might have not been attempting at the right way around my problem by using a KeyListener.

Bahramdun Adil

You can use the KeyEvent to get the key code, and then check it.

E.g:

someCompunent.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_MINUS) {
           // do the stuff
        }
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I check if the user is pressing a key?

From Dev

How do I check a key press in Java?

From Dev

C++ How do I read in input for a certain amount of time only? Can I do so without the user pressing enter?

From Dev

How do I call a button click by pressing the Enter key in Powershell

From Dev

How do I call a button click by pressing the Enter key in Powershell

From Dev

How do I check for an object at a certain position?

From Dev

How do I check for an object at a certain position?

From Dev

How do I check if a certain User exists under a specific Login in SQL?

From Dev

How do I check if a certain User exists under a specific Login in SQL?

From Dev

Pressing the tilde key (~) waits for a second key stroke, how do I disable this?

From Dev

Pressing the tilde key (~) waits for a second key stroke, how do I disable this? (2020 version)

From Dev

How do I check which key is pressed?

From Dev

How do I check which key is pressed?

From Dev

java- How do I create a String check with a delay of a certain amount of time?

From Dev

Unity3D: How do i change force when pressing 1 key then pressing another at the same time?

From Dev

how do I create a program in windows that can be activated by pressing short cut key instead of clicking?

From Dev

How do I automate pressing the F5 key using vbscript?

From Dev

How do i get windows' view like on Fedora, where pressing super key?

From Dev

How do I automate pressing the F5 key using vbscript?

From Dev

In Emacs, How do I configure to delete matching bracket just pressing delete key?

From Dev

How do I pick an array position when pressing a key for example a number from 1 to 5?

From Java

How do I check in JavaScript if a value exists at a certain array index?

From Dev

How do I check the number of occurences of a certain string in another string?

From Dev

How do I check if a string has exactly one of a certain character

From Dev

SpriteKit - How do I check if a certain set of coordinates are inside an SKShapeNode?

From Dev

How do I check if int[] contains only certain numbers?

From Dev

How do i check if an Array List contains a certain string

From Dev

AutoHotKey: How do I check for the presence of a button with a certain label?

From Dev

How do I check if a file exists inside a certain directory in Batch?

Related Related

  1. 1

    How do I check if the user is pressing a key?

  2. 2

    How do I check a key press in Java?

  3. 3

    C++ How do I read in input for a certain amount of time only? Can I do so without the user pressing enter?

  4. 4

    How do I call a button click by pressing the Enter key in Powershell

  5. 5

    How do I call a button click by pressing the Enter key in Powershell

  6. 6

    How do I check for an object at a certain position?

  7. 7

    How do I check for an object at a certain position?

  8. 8

    How do I check if a certain User exists under a specific Login in SQL?

  9. 9

    How do I check if a certain User exists under a specific Login in SQL?

  10. 10

    Pressing the tilde key (~) waits for a second key stroke, how do I disable this?

  11. 11

    Pressing the tilde key (~) waits for a second key stroke, how do I disable this? (2020 version)

  12. 12

    How do I check which key is pressed?

  13. 13

    How do I check which key is pressed?

  14. 14

    java- How do I create a String check with a delay of a certain amount of time?

  15. 15

    Unity3D: How do i change force when pressing 1 key then pressing another at the same time?

  16. 16

    how do I create a program in windows that can be activated by pressing short cut key instead of clicking?

  17. 17

    How do I automate pressing the F5 key using vbscript?

  18. 18

    How do i get windows' view like on Fedora, where pressing super key?

  19. 19

    How do I automate pressing the F5 key using vbscript?

  20. 20

    In Emacs, How do I configure to delete matching bracket just pressing delete key?

  21. 21

    How do I pick an array position when pressing a key for example a number from 1 to 5?

  22. 22

    How do I check in JavaScript if a value exists at a certain array index?

  23. 23

    How do I check the number of occurences of a certain string in another string?

  24. 24

    How do I check if a string has exactly one of a certain character

  25. 25

    SpriteKit - How do I check if a certain set of coordinates are inside an SKShapeNode?

  26. 26

    How do I check if int[] contains only certain numbers?

  27. 27

    How do i check if an Array List contains a certain string

  28. 28

    AutoHotKey: How do I check for the presence of a button with a certain label?

  29. 29

    How do I check if a file exists inside a certain directory in Batch?

HotTag

Archive