Stange problem: Leonardo to trigger keyboard command in Ubuntu [Partly solved]

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.

The code I use is this:

   Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press("s");
    delay(100);
    Keyboard.releaseAll();

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 presume you meant:

    Keyboard.press('s');

What you had didn't compile, which is why we don't like it when you just post a snippet, and one that has been retyped, at that.

http://snippets-r-us.com/

I tested your lines (with "s" changed to 's') and on my Ubuntu PC it worked.

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);

    }
  }
}

Well, can you test without all the PIR stuff, like just make it activated by a switch?

This code works fine on my Windows PC but not on Ubuntu. I do not understand it.

Simplified code:

void setup(){
     //Serial.begin(9600);
     Keyboard.begin();
     USBDevice.attach();
}

void loop()
{
      delay(15000);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_DELETE);
      delay(200);
      Keyboard.releaseAll();
      while(true);
}

ole@ubuntu:~$lsusb Bus 001 Device 007: ID 2341:8036 Arduino SA Leonardo (CDC ACM, HID)

ole@ubuntu:~$ ls -l /dev/bus/usb/001/007 crw-rw-r-- 1 root usbusers 189, 6 des. 5 23:21 /dev/bus/usb/001/007

void setup(){
     //Serial.begin(9600);
     Keyboard.begin();
     USBDevice.attach();
}

void loop()
{
      delay(15000);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_DELETE);
      delay(200);
      Keyboard.releaseAll();
      delay(500);
      Keyboard.println("This works fine!!!");
      while(true);
}

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)

Has anyone else experienced this? I do not know if the problem is related to Arduino, Ubuntu permissions or my pc hardware.

No one?

Put it like this: It worked for me on Ubuntu, and it sounds like no-one else has the problem.

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.