How can i determine which shift/ctrl/alt key is being pressed? [SOLVED]

Currently trying to figure out how to capture keys which have multiple locations on a typical US layout keyboard, this is just some code I’ve been using to test if this idea can work, and how I should do it.

import java.awt.event.KeyEvent;
boolean[] Key = new boolean[ 65536 ];

void setup(){
  //registerMethod("keyEvent", this);  
}

void draw() {
  background( 0 );
} // Empty draw() needed to keep the program running

void keyPressed() {
  int KeyLocation = KeyEvent.getKeyLocation();
  if( !Key[ keyCode ] ){
    println("pressed " + int(key) + " " + keyCode + " " + key);
    Key[ keyCode ] = true;
  }
}

void keyReleased() {
  println("released " + int(key) + " " + keyCode + " " + key);
    Key[ keyCode ] = false;
}

//void keyEvent( KeyEvent event ){
//  int KeyLocation = event.getKeyLocation();
//}

I’ve looked for 5 hours, tried anything and everything mentioned, and haven’t had any luck.

These are some resources I found while looking. ( ps, i’m using processing 3.5.4, so a lot of these are likely out of date)

  1. java - Distinguishing left and right shift keys in Processing 3 - Stack Overflow
  2. The Shift keys? - Processing Forum

The error I’m encountering is i can either import java.awt.event.KeyEvent, and have getKeyLocation() be call able, but not run, or return anything; or, if i dont import java.awt.event.KeyEvent, that function does not exist (obviously because it can’t be called in).
So, I need to import java.awt.event.KeyEvent, and figure out how to ask getKeyLocation() nicely for the location of the key pressed.

1 Like

Processing’s keyPressed() function has an overloaded version which comes with a processing.event.KeyEvent instance as parameter. You must use it and call this instance’s getNative() method in order to summon its source object, which varies depending on the renderer you choose for your sketch. If you’re using the default renderer, JAVA2D, you can cast the source object to java.awt.event.KeyEvent. Then you’ll be able to call getKeyLocation() and any other java.awt.event.KeyEvent methods. This works the same for keyReleased(KeyEvent e) and keyTyped(KeyEvent e), and has an analogous work on mousePressed(MouseEvent e), release…, clicked… etc, for java.awt.event.MouseEvent, MouseAdapter, etc…

Here’s an example with the default renderer (JAVA2D):

import java.awt.event.KeyEvent;

void setup() {
}

void draw() {
}

void keyPressed(processing.event.KeyEvent event) {
    KeyEvent nativeEvent = (KeyEvent) event.getNative();

    if (event.isShiftDown()) {
        switch (nativeEvent.getKeyLocation()) {
        case KeyEvent.KEY_LOCATION_STANDARD:
            println("standard shift");
            break;
        case KeyEvent.KEY_LOCATION_LEFT:
            println("left shift");
            break;
        case KeyEvent.KEY_LOCATION_RIGHT:
            println("right shift");
            break;
        case KeyEvent.KEY_LOCATION_NUMPAD:
            println("numpad shift");
            break;
        case KeyEvent.KEY_LOCATION_UNKNOWN:
            println("unknown shift");
            break;
        }
    }
}

I hope it helped you :wink:

4 Likes

It worked wonderfully, thank you. I don’t know any java really, so I was struggling. Thanks again.

2 Likes

Nice! I’m glad it helped you. Please flag your post as “solved” so that people with similar questions can find it :wink:

1 Like

import java.awt.event.KeyEvent;

String KeyLocation = “”;
String KeyLocationReleased = “”;
String KeyString = “”;
String KeyStringReleased = “”;
String KeyPressed = { “” };
int Keys = { 0 };
boolean KeyFoundInList = false;

void setup(){
size( 1000, 500 );
textSize( 10 );
}

void draw() {
background( 0 );
text( KeyPressed.length, 200, 10 );
for ( int i = 0; i < KeyPressed.length; i ++ ) {
text( KeyPressed[ i ], 100, ( i + 1 ) * 10 );
}
Keys = expand( Keys, KeyPressed.length );
}

void keyPressed( processing.event.KeyEvent event ) {
KeyEvent nativeEvent = ( KeyEvent ) event.getNative();
switch ( nativeEvent.getKeyLocation() ) {

case KeyEvent.KEY_LOCATION_STANDARD:
  KeyLocation = "Standard";
  break;
  
case KeyEvent.KEY_LOCATION_LEFT:
  KeyLocation = "Left";
  break;
  
case KeyEvent.KEY_LOCATION_RIGHT:
  KeyLocation = "Right";
  break;
  
case KeyEvent.KEY_LOCATION_NUMPAD:
  KeyLocation = "Numpad";
  break;
  
case KeyEvent.KEY_LOCATION_UNKNOWN:
  KeyLocation = "Unknown";
  break;
  
default:
  KeyLocation = "Defaulted";
  break;

}
KeyString = KeyLocation + " " + nativeEvent.getKeyText( nativeEvent.getKeyCode() );
//println( KeyString );
KeyFoundInList = false;
for ( int i = 0; i < KeyPressed.length; i ++ ) {
if ( KeyString.equals( KeyPressed[ i ] ) ){
KeyFoundInList = true;
break;
}
}
if ( !KeyFoundInList ) {
KeyPressed = append( KeyPressed, KeyString );
}
}

void keyReleased( processing.event.KeyEvent event ) {
KeyEvent nativeEvent = ( KeyEvent ) event.getNative();
switch ( nativeEvent.getKeyLocation() ) {

case KeyEvent.KEY_LOCATION_STANDARD:
  KeyLocationReleased = "Standard";
  break;
  
case KeyEvent.KEY_LOCATION_LEFT:
  KeyLocationReleased = "Left";
  break;
  
case KeyEvent.KEY_LOCATION_RIGHT:
  KeyLocationReleased = "Right";
  break;
  
case KeyEvent.KEY_LOCATION_NUMPAD:
  KeyLocationReleased = "Numpad";
  break;
  
case KeyEvent.KEY_LOCATION_UNKNOWN:
  KeyLocationReleased = "Unknown";
  break;
  
default:
  KeyLocationReleased = "Defaulted";
  break;

}
KeyStringReleased = KeyLocationReleased + " " + nativeEvent.getKeyText( nativeEvent.getKeyCode() );
for ( int i = 0; i < KeyPressed.length; i ++ ) {
if ( KeyStringReleased.equals( KeyPressed[ i ] ) ){
KeyPressed = concat( subset( KeyPressed, 0, i ), subset( KeyPressed, i + 1, KeyPressed.length - ( i + 1 ) ) );
break;
}
}
}

just came up with this mess; it reports correctly which keys are being pressed, along with their locations into the array KeyPressed, based on which key was hit first.