We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Virtual Keypress (Read 2637 times)
Virtual Keypress
Dec 18th, 2008, 7:28pm
 
It is pretty straightforward to recognize an actual keypress with Processing, but is it possible to tell Processing to press a key?  I.E. I want certain events to turn on capslock, num lock, and scroll lock.

Specifically, I'm trying to make a simple switch, and can't wait for an arduino to come in the mail, so I was thinking I could use the led's in the keyboard as controllers (suggested here:  http://www.instructables.com/id/Control-Electrical-Stuff-with-your-compu ter/).  I'm a newbie...so any suggestions would be much appreciated, especially for a less roundabout way of doing this.
Re: Virtual Keypress
Reply #1 - Dec 18th, 2008, 8:09pm
 
I tried this:

Code:

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

Toolkit t = Toolkit.getDefaultToolkit();


Boolean b = t.getLockingKeyState(KeyEvent.VK_CAPS_LOCK);


println(b);


t.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, false);
t.setLockingKeyState(KeyEvent.VK_NUM_LOCK, true);


This will produce:

Code:


false


Exception in thread "Animation Thread" java.lang.UnsupportedOperationException: Toolkit.setLockingKeyState
at java.awt.Toolkit.setLockingKeyState(Toolkit.java:1334)
at sketch_dec18b.setup(sketch_dec18b.java:32)
at processing.core.PApplet.handleDraw(PApplet.java:1383)
at processing.core.PApplet.run(PApplet.java:1311)
at java.lang.Thread.run(Thread.java:613)



so, it can read the state, but can't use set statement for some reason.  This isn't limited to Processing (tried it outside of PApplet as well).


Anyone know how to get around this?
Re: Virtual Keypress
Reply #2 - Dec 18th, 2008, 8:35pm
 
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html

the Robot class has functions for controlling keyboard and mouse-manipulation

-seltar
Re: Virtual Keypress
Reply #3 - Dec 18th, 2008, 8:42pm
 
and since i was bored
Code:

final int CAPS_LOCK = 20;
final int NUM_LOCK = 144;
final int SCROLL_LOCK = 145;


Robot r;
void setup()
{
size(100,100);
try{
r = new Robot();
}catch(AWTException a){}
}

void draw()
{
int v = (int)random(3);
switch(v){
case 0:
r.keyPress(CAPS_LOCK);
r.keyRelease(CAPS_LOCK);
break;
case 1:
r.keyPress(NUM_LOCK);
r.keyRelease(NUM_LOCK);
break;
case 2:
r.keyPress(SCROLL_LOCK);
r.keyRelease(SCROLL_LOCK);
break;
}
}
Re: Virtual Keypress
Reply #4 - Dec 18th, 2008, 8:58pm
 
Good to know!
Re: Virtual Keypress
Reply #5 - Dec 18th, 2008, 9:53pm
 
Wow.  Thanks for the fast responses, very much appreciated.
Page Index Toggle Pages: 1