Telling the computer to hold down a key

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?

1 Like

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. I have the calsign demo running on a Mac if you have problems converting it.

1 Like

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.

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.

So the Computer thinks ctrl is down all the time

That would be the goal

1 Like

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

1 Like

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?

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

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



// 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 
  //
}
//

1 Like

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

1 Like

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:

   surface.setAlwaysOnTop(true);
1 Like

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.

1 Like