# Telling the computer to hold down a key

**URL:** https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945
**Category:** Project Guidance
**Created:** [February 2, 2022, 10:18pm UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945 "2022-02-02T22:18:35Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [February 2, 2022, 10:18pm UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/1 "2022-02-02T22:18:35Z")

</div>

I have a friend who is disabled and can only use a commercial head mounted mouth joystick. With this he can use his tong to move the mouse pointer, and a blow down the tube for a click.

Unfortunately he can’t do a double click, nor a right click.

I was wondering if there was a way the Processing could somehow communicate with the computer so that the computer thought the ctrl key was being held down. That way he could click on a processing window that would hold down or release the ctrl key, in much the same was as the shift lock works. This is for a Mac computer.

I can’t think of any way of doing this, is it possible?

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [February 3, 2022, 2:37am UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/2 "2022-02-03T02:37:27Z")

</div>

Is this similar to what you are wanting to do: [https://forum.processing.org/two/discussion/4583/how-do-i-generate-a-keypress-maybe-robot.html](https://forum.processing.org/two/discussion/4583/how-do-i-generate-a-keypress-maybe-robot.html). I have the calsign demo running on a Mac if you have problems converting it.

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [February 3, 2022, 6:48am UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/3 "2022-02-03T06:48:34Z")

</div>

Thanks for that.  
I have had a look but it seems there is no key code for the ctrl key.  
From what I can gather ctrl and shift are modifiers that can only be sent in conjunction with a key code.  
What I need to do is to is to have the modifier ctrl key held so that a click on a file becomes a ctrl click.  
This is what I have so far, only using the “s” key.

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

Robot robot;
boolean ctrlHold = false;

void setup() {
  size(100, 100);

  //Let's get a Robot...
  try { 
    robot = new Robot();
  } catch (AWTException e) {
    e.printStackTrace();
    exit();
  }
}

void draw() {
  if(ctrlHold) {
    background(#ff0000); // red
  }
  else {
    background(#ffffff); // white
  }
}

void mousePressed() {
  //hold the S key down
  if(ctrlHold) {
    robot.keyRelease(KeyEvent.VK_S);
    ctrlHold = false;
  }
  else {
   robot.keyPress(KeyEvent.VK_S);
   ctrlHold = true;
  }
}

  void keyPressed() {
    println("Key pressed: " + key); 
}

```

I can see it is working for the S key by dragging the keyboard view next to the Processing window and toggling the S key by clicking the processing window.

 ![Working for s key](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a61529ca6e742b2bb51d41e1cad4afccd5a808a2.png)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 3, 2022, 7:46am UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/4 "2022-02-03T07:46:06Z")

</div>

So the Computer thinks ctrl is down all the time

That would be the goal

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [February 3, 2022, 7:47am UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/5 "2022-02-03T07:47:56Z")

</div>

> [@Chrisir](#):
>
> So the Computer thinks ctrl is down all the time

Correct, so we can turn a left click to a right one.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 3, 2022, 8:05am UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/6 "2022-02-03T08:05:04Z")

</div>

> so we can turn a left click to a right one.

Mouse click you mean?

So we could also simulate a right mouse click (instead of using ctrl) ?

Like say a switch from now on a left mouse is sent like a right mouse click?

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [February 3, 2022, 10:26am UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/7 "2022-02-03T10:26:30Z")

</div>

1. Are you a Mac user?
2. Right click on a Mac is a control-click and as far as I know all that gets the user is a contextual menu with menu items such as cut, copy, paste and a few other goodies depending on the app.
3. What do you hope to achieve with a right click?
4. There is a KeyEvent.VK\_CONTROL and a KeyEvent.CTRL\_DOWN\_MASK but I’m not sure you can asynchronously connect them with a mouse event using Robot.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 3, 2022, 1:09pm UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/8 "2022-02-03T13:09:41Z")

</div>

this works for me under WIN

you have two buttons

- for control / CTRL key start (which stays down/on, at least under WIN) and
- control / CTRL key stop

Between these, the computer thinks that you permanently hold the _control / CTRL key_ down.

Chrisir

* * *

```auto

// press and hold CTRL 

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

Robot robot;

void setup() {
  size(400, 400);

  //Let's get a Robot...
  try { 
    robot = new Robot();
  } 
  catch (AWTException e) {
    e.printStackTrace();
    exit();
  }
}

void draw() {
  background(#ffffff); // white

  stroke(0);
  fill(255); 
  ellipse(0, 0, 200, 200);
  fill(0); 
  text("CTRL on", 18, 39); 

  stroke(0);
  fill(255);
  ellipse(0, height, 200, 200);
  fill(0); 
  text("CTRL off", 18, height-39);
}

void mousePressed() {
  // eval 2 buttons 
  // https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html 

  if (dist(mouseX, mouseY, 0, 0) < 200) {
    //Presses a given key. The key should be released using the keyRelease method. 
    robot.keyPress(17);
  } else if (dist(mouseX, mouseY, 0, height) < 200) {
    // Releases a given key. 
    robot.keyRelease(17);
  } else {
    //
    if (mouseButton==LEFT ) 
      println ("left"); 
    if (mouseButton==RIGHT ) 
      println ("right");
  }//else 
  //
}
//

```

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [February 4, 2022, 11:34am UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/9 "2022-02-04T11:34:18Z")

</div>

@Chrisir  
Fantastic this works on my Mac as well.  
As far as I can tell this should be fine for my friend. I will try and get it to him shortly for testing but it seems to do everything I asked for.  
Many thanks.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 4, 2022, 5:10pm UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/10 "2022-02-04T17:10:08Z")

</div>

> [@Chrisir](#):
>
> ```auto
> if (mouseButton==LEFT ) 
> println ("left"); 
> if (mouseButton==RIGHT ) 
> println ("right");
> 
> ```

Can be removed obviously

**Remark**

In theory it would be cool if we could tell the Sketch to be always on top

Then it’s better accessible while working in other programs

Its window can be quite small theoretically

This could be it, not tested:

```auto
   surface.setAlwaysOnTop(true);

```

---

<div class="post-metadata">

### Author: ![borjonx](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@borjonx](https://discourse.processing.org/u/borjonx)
#### Post date: [February 9, 2022, 11:40pm UTC](https://discourse.processing.org/t/telling-the-computer-to-hold-down-a-key/34945/11 "2022-02-09T23:40:37Z")

</div>

> [@Chrisir](#):
>
> In theory it would be cool if we could tell the Sketch to be always on top

I have used the robot class to control mouse and keyboard using my voice, and using depth values from the kinect 3D camera - no matter which other app is active the Processing sketch still moves the mouse, clicks it and sends keystrokes. This is a fun way to play w/paint programs.
