Davis Diercks
Davis Diercks

Reputation: 679

Distinguishing left and right shift keys in Processing 3

Alright, I'll say in advance I'm aware this isn't a new concept... But no matter what I research nothing seems to work. Basically, I want to be able to sense every key on my keyboard including the different shift/ctrl/alt/enter keys. Every key besides these returns a unique keyCode which is good, but I can't seem to distinguish these duplicates.

Without any modifications, the void keyPressed () will work just fine. I'm told that to distinguish the duplicate keys I can import java.awt.event.KeyEvent; and then use

void keyPressed (KeyEvent e) {
  if (keyCode == SHIFT) {
    int location = e.getKeyLocation ();
    if (location == KEY_LOCATION_RIGHT) {
      RShift = true;
    }
    if (location == KEY_LOCATION_LEFT) {
      LShift = true;
    }
  }
}

However, some problems arise with this:

Do I need like a reverse override or something?? Help is much appreciated!

P.S. Another related question, how can I distinguish more than left, center, and right mouse buttons? I can get these and the scrollwheel but any other button just returns a mouseButton code of 0. Suggestions? Thanks!

Upvotes: 0

Views: 749

Answers (1)

J dev
J dev

Reputation: 66

https://processing.org/reference/keyPressed_.html

The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable.

if you want to override keyPressed you must use the same signature so no parameters, in the method you can reference the key variable of the PApplet

like this i believe

void keyPressed ()
**int location = key

edit: int location = keyEvent

Upvotes: 0

Related Questions