# How do i use a variable to press a key with robot class

**URL:** https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752
**Category:** Coding Questions
**Created:** [April 30, 2021, 5:09pm UTC](https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752 "2021-04-30T17:09:47Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![tobich](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@tobich](https://discourse.processing.org/u/tobich)
#### Post date: [April 30, 2021, 5:09pm UTC](https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752/1 "2021-04-30T17:09:47Z")

</div>

**Basically I’m trying to make a function that will type the key i need**

Because its a function i give it a string with a variable which is the key i need to press and the robot variable how do i do it?

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 30, 2021, 5:26pm UTC](https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752/2 "2021-04-30T17:26:03Z")

</div>

I’m sorry but I don’t understand what is it you try to do.

---

<div class="post-metadata">

### Author: ![tobich](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@tobich](https://discourse.processing.org/u/tobich)
#### Post date: [April 30, 2021, 5:29pm UTC](https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752/3 "2021-04-30T17:29:42Z")

</div>

Yea i dont really do too so  
I need a function to press the button i need which will do something like this

```auto
void SendKey(String KeyINeed, Robot robot) {
  robot.keyPress(KeyEvent.VK_(key);
  robot.keyRelease(KeyEvent.VK_(key);
}

```

so pressing a key and releasing it

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 30, 2021, 6:23pm UTC](https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752/4 "2021-04-30T18:23:59Z")

</div>

Still not sure I get what you want but I guess you are looking for the [keyPressed()](https://processing.org/reference/keyPressed_.html) function.

---

<div class="post-metadata">

### Author: ![tobich](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@tobich](https://discourse.processing.org/u/tobich)
#### Post date: [April 30, 2021, 6:33pm UTC](https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752/5 "2021-04-30T18:33:30Z")

</div>

i dont know how to explain it but  
i have a function that should press a key that’s specified when it gets called  
so the caller looks like this:

```auto
Presskey(robot, S)

```

and the PressKey function should press and release the second variable S in this example

---

<div class="post-metadata">

### Author: ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)
#### Post date: [April 30, 2021, 6:50pm UTC](https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752/6 "2021-04-30T18:50:44Z")

</div>

hi,  
do you use  
`import java.awt.Robot;` ?  
where do you want to press a key? in an another app?

---

<div class="post-metadata">

### Author: ![tobich](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@tobich](https://discourse.processing.org/u/tobich)
#### Post date: [April 30, 2021, 6:52pm UTC](https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752/7 "2021-04-30T18:52:31Z")

</div>

Yes i am using it.

I want to emulate a keyboard press to make it work in another app

---

<div class="post-metadata">

### Author: ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)
#### Post date: [April 30, 2021, 7:15pm UTC](https://discourse.processing.org/t/how-do-i-use-a-variable-to-press-a-key-with-robot-class/29752/8 "2021-04-30T19:15:27Z")

</div>

ok,  
here is an example:

```auto
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent; 

Robot robot;
String keyString="hello world";
void setup() {

  size(100, 100);
  try { 
    robot = new Robot(); 
  } 
  catch (AWTException ex) {
    System.out.println(ex.getMessage());
  }
  frameRate(1);
}

void draw() {
  sendKeys(robot, keyString);
}

void sendKeys(Robot robot, String keys) {
  for (char c : keys.toCharArray()) {
    int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
    if (KeyEvent.CHAR_UNDEFINED == keyCode) {
      throw new RuntimeException(
        "Key code not found for character '" + c + "'");
    }
    robot.keyPress(keyCode);
    robot.delay(100);
    robot.keyRelease(keyCode);
    robot.delay(100);
  }
}

```

run the sketch then go back to processing ide
