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

**URL:** https://discourse.processing.org/t/how-can-i-determine-which-shift-ctrl-alt-key-is-being-pressed-solved/36579
**Category:** Coding Questions
**Created:** [April 24, 2022, 8:38pm UTC](https://discourse.processing.org/t/how-can-i-determine-which-shift-ctrl-alt-key-is-being-pressed-solved/36579 "2022-04-24T20:38:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![RedDeath](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@RedDeath](https://discourse.processing.org/u/RedDeath)
#### Post date: [April 24, 2022, 8:38pm UTC](https://discourse.processing.org/t/how-can-i-determine-which-shift-ctrl-alt-key-is-being-pressed-solved/36579/1 "2022-04-24T20:38:35Z")

</div>

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.

```auto
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](https://stackoverflow.com/questions/41091411/distinguishing-left-and-right-shift-keys-in-processing-3)
2. [The Shift keys? - Processing Forum](https://forum.processing.org/one/topic/the-shift-keys.html)

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.

---

<div class="post-metadata">

### Author: ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)
#### Post date: [April 25, 2022, 1:13am UTC](https://discourse.processing.org/t/how-can-i-determine-which-shift-ctrl-alt-key-is-being-pressed-solved/36579/2 "2022-04-25T01:13:13Z")

</div>

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):

```auto
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 😉

---

<div class="post-metadata">

### Author: ![RedDeath](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@RedDeath](https://discourse.processing.org/u/RedDeath)
#### Post date: [April 28, 2022, 3:42am UTC](https://discourse.processing.org/t/how-can-i-determine-which-shift-ctrl-alt-key-is-being-pressed-solved/36579/3 "2022-04-28T03:42:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)
#### Post date: [April 28, 2022, 9:02am UTC](https://discourse.processing.org/t/how-can-i-determine-which-shift-ctrl-alt-key-is-being-pressed-solved/36579/4 "2022-04-28T09:02:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RedDeath](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@RedDeath](https://discourse.processing.org/u/RedDeath)
#### Post date: [May 22, 2022, 4:23pm UTC](https://discourse.processing.org/t/how-can-i-determine-which-shift-ctrl-alt-key-is-being-pressed-solved/36579/5 "2022-05-22T16:23:36Z")

</div>

> 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.
