I have a strange problem in Ubuntu. I want Leonardo to send a keyboard command (Alt+Ctrl+s) to trigger suspend. (I have created a keyboard shortcut to trigger suspend with Alt+Ctrl+s which works fine with a normal keyboard.) However I cannot get this working. I manage to get the Leonardo to send keyboard presses which are displayed in a text editor. However I cannot get Leonardo to trigger a command. I suspect it has something to do with run rights of the Leonardo keyboard in Ubuntu.
I have tried this in Ubuntu 10.04, 12.04 and 13.10 with same result. I have also tried with shorter, longer and no delay.
I have tried the Arduino Keyboard Logout tutorial with same result.http://arduino.cc/en/Tutorial/KeyboardLogout The Ubuntu will not trigger Logout (CTRL+ALT+DEL). The same program works fine on a Windows 8 PC using the same Leonardo board.
I have so far spent 2 days to find a solution so far and I am now on a dead end. Please help me...
I am sorry to not include my whole code. I had my code on another PC used by my wife. In the code I used the ' and not the "
Here is the code: I use a PIR to trigger the suspend.
int pirPin = 2; //digital 2
unsigned long currentTime = 0; //this variable will be overwritten by millis() each iteration of loop
unsigned long pastTime = 0; //no time has passed yet
boolean pcOn = true; //the default state
long wait = 15000; //Time to trigger suspend after last motion detected
void setup(){
//Serial.begin(9600);
Keyboard.begin();
USBDevice.attach();
pinMode(pirPin, INPUT);
currentTime = millis(); //currentTime is now the current time (again).
pastTime = currentTime; //currentTime at this point is the current time from the previous iteration, this should now be pastTime
}
void loop()
{
int pirVal = digitalRead(pirPin);
currentTime = millis(); //currentTime is now the current time (again).
unsigned long timePassed = currentTime - pastTime; //this is roll-over proof, if currentTime is small, and pastTime large, the result rolls over to a small positive value, the time that has passed
if(pirVal == LOW){ //was motion detected
//Serial.println("Motion Detected");
//Serial.println("timepassed:");
//Serial.println(timePassed);
pastTime = currentTime; //resetter tid
timePassed = 0;
if(pcOn==false){
//USBDevice.wakeupHost();
UDCON |= (1 << RMWKUP); // send the wakeup request
//Serial.println("PC on");
delay(10000);
pcOn = true;
}
}
if(pcOn){
if(timePassed >= wait) //part1 of the state engine
{
//Serial.println("PC suspend");
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('s');
delay(200);
Keyboard.releaseAll();
pcOn = false;
delay(10000);
}
}
}
The strange thing is that "This works fine!!" show up in a texteditor on the ubuntu PC. I do not understand why this is not working. The ubuntu PC is a Zotac Zbox ID83 (intel i3)
This makes it even stranger. I have tried 3 clean and new ubuntu releases with the same result. Then it sounds like a hardware PC problem which is strange. The ubuntu pc does receive normal keystrokes.
I have found a work around. I use Keyboard.write(KEY_F12) to trigger the suspend. I do not manage to get a combination of keys (ctrl+alt+...) to work in ubuntu with Keyboard.press. Ubuntu registrers the multiple key presses however it will not execute a command.
There must be something wrong/strange in the USB protocol on the Leonardo or how Ubuntu translates the signal.