0

I was wondering if it is possible to create an even in java, without constantly polling the state of the value I am observing.

For example, is it possible for me to create an event that takes place when the user presses the space bar, without checking if the space bar is pressed, so that a function would be called only if the user presses the space bar?

  • what do you mean by "constantly polling the state of the value I am observing"? – Sher Alam Feb 10 '16 at 09:19
  • Yeah, there is you have to add a keylistener to your parent component which will direct the already existing keypressevent to your component. https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html – HopefullyHelpful Feb 10 '16 at 09:21
  • the principles are described in https://en.wikipedia.org/wiki/Observer_pattern the observer pattern.... – Martin Frank Feb 10 '16 at 09:21
  • @Sher Alam By that I mean, that I want an event to happen when a value changes, e.g. the value of a key was "false" because it was not pressed and changes to true, when the user presses it, without having to write a while-loop that periodically checks whether the key is pressed or not. –  Feb 10 '16 at 09:24
  • @Martin Frank But the problem of notifying the observers, only when the key changes from not being pressed to being pressed without checking the state in a while loop still remains. –  Feb 10 '16 at 09:26
  • Do you want this to happen in console mode? If you do, then i sorry i fear there is no other method than to use a while loop. Otherwise if want all this for a component like `JTextfield` or `JPanel` etc then you can add a `KeyListener` to them. – Sher Alam Feb 10 '16 at 09:28
  • Does the key listener poll the state of the keys in a while loop? –  Feb 10 '16 at 09:29
  • You can refer to the java documentation. [line](https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html) – Sher Alam Feb 10 '16 at 09:30
  • sorry for replying so late: i was merely pointing out how to solve such a problem in **general* - i didn't know your question was specific on `KeyEvent` (i thought that was just an example, as described in your question) – Martin Frank Feb 10 '16 at 09:56
  • if you are not sure wether to poll or observe, you can read the article http://programmers.stackexchange.com/questions/102771/when-would-polling-for-events-be-better-than-using-observer-pattern ... – Martin Frank Feb 10 '16 at 09:58
  • Internal implementation might have used the looping mechanism, but a listener means a waiting mechanism for an event, when it happens the system will notify all the listeners register for that event. – Sher Alam Feb 10 '16 at 10:09
  • are you trying to 'read the key status' from an GUI (a Frame either swing/awt/swt...) or from console? – Martin Frank Feb 10 '16 at 10:16

3 Answers3

0

Using Observer pattern, Java already implemented. Check Java document or here.

S Dao
  • 555
  • 4
  • 7
0

A boolean flag

boolean flag = false;

Here is a JTextField.

JTextField tf = new JTextField();

And you add a Key Listener to it in order to listen for key events on it.

 tf.addKeyListener(kl);

kl is an object of KeyListener which you create earlier.

KeyListener kl = new KeyListener(){
   ...
   onKeyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_SPACE) {
         System.out.println("space hit");
         flag = !flag;      //I think this is what you want
        }
     }
     ...
};

This is not a working code. Just an example to let you better understand Listeners.

Sher Alam
  • 372
  • 2
  • 9
0

since default OS provide only key-event-driven support you have to write your own keyboard driver where you can provide methods to poll the key status...

(remember, you don't have hardware pins on your keyboard for each key that can be checked like you're used to do on microcontroller, the keyboard itself handles the states and sends events to the OS, which on its side calls 'interrupts'. these interups are handled by the driver, namely keyboard driver)

Writing a keyboard device driver

you can more easiliy write your own keyboard class that maps all keyevents and can be polled

class KeyboardState extends KeyAdapter{

    boolean[] keyState = new boolean[256];

    @Override
    public void keyPressed (KeyEvent ke){
        keyState[ke.keyCode] = true;
    }

    @Override
    public void keyReleased(KeyEvent ke){
        keyState[ke.keyCode] = false;
    }

    public boolean isKeyDown(int keyCode){
        return keyState[keyCode];
    }
}

this code has been directly written in SO so no validation... don't forget to add you keyboard to the according frame...

(if you're listening to console input let me know - this answer wont fit in then)...

Community
  • 1
  • 1
Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • wouldnt it be `keyEvent.getKeyCode()` ? i think so, please correct that in your code ^_^ haha, have fun, happy coding! – Martin Frank Feb 10 '16 at 10:25