Send key strokes via serial.write

I am trying to send ctrl+k keystroke through serial write to my computer. I am having troubles finding the correct HEX, ASCII, or Decimal for this. I get a compiler error when working my way. I understand that this is not a Leonardo or a FLASH DFU Arduino.

I have successfully sent the space command. I am using an additional program ACC Keys to capture the serial data.

Arduino UNO R3
IDE Arduino 1.0.6
Using Arduino: 1 Week :smiley:

I would appreciate any help with either the proper search topic or help with code. I have spent two days looking for the key command without sending to a scanner or LCD display.

I don't understand the error and warning yet. I am sorry if this has been asked already.

Thank you for any help.

My Code

// Function changeColor will send the keystroke 'CTRL+K' to the computer
void changeColor () {
  int KeyCommand[] = {0};
  KeyCommand[0] = 11;
  Serial.write(KeyCommand, 8);
}

Error

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
Photobooth2.ino: In function 'void changeColor()':
Photobooth2:98: error: no matching function for call to 'HardwareSerial::write(int [1], int)'
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:62: note: candidates are: virtual size_t HardwareSerial::write(uint8_t)
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:63: note:                 size_t HardwareSerial::write(long unsigned int)
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:64: note:                 size_t HardwareSerial::write(long int)
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:65: note:                 size_t HardwareSerial::write(unsigned int)
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:66: note:                 size_t HardwareSerial::write(int)
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Print.h:54: note:                 size_t Print::write(const char*, size_t)
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Print.h:53: note:                 virtual size_t Print::write(const uint8_t*, size_t)
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)

Photobooth2.ino (3.7 KB)

I have no experience with ACC keys...
But maybe this will help FaceBooth

tahikib:
I am trying to send ctrl+k keystroke through serial write to my computer. I am having troubles finding the correct HEX, ASCII, or Decimal for this.

The control key is a modifier which, in ASCII, clears the 2 most significant bits of the 7 bit character code, the modifier key is applied to.

As you seem to have worked out.
'K' = 107 = 0x6B = &b01101011
Ctrl/K = 11 = 0x0B = &b00001011

Modern keyboards use a scan code for the modifier keys and I have no idea whether the ASCII values will work for your application.

The problem with your code is unrelated. The compiler error is being generated because you are calling the Serial.write() function incorrectly.

// Function changeColor will send the keystroke 'CTRL+K' to the computer
void changeColor () {
  int KeyCommand[] = {0};                //<-  here you have declared an array of one integer type (2 bytes)
  KeyCommand[0] = 11;                     //<-   and you assign one character type (1 byte)
  Serial.write(KeyCommand, 8);           //<-  But, you have told the write function to expect 8 bytes
}

Were you to look at the source for the Serial library, you would see that there is more than one Serial.write() function defined. Each version takes different 'types' of parameters, which the compiler matches to your function call at compile time. The compiler is choking, because what you have written does not match any version of Serial.write();

This is what you meant to do.

  char KeyCommand = 11;      //an ASCII code is a character type (1 byte)
  Serial.write(keyCommand);   //write one character to the serial port

The compiler would also accept this, although it's not what you want to do

  char someChars[] = {72, 69, 76, 76, 79, 32, 87, 79, 82, 76, 68};
  Serial.write(someChars, 11);  //write 11 chars to the serial port

tahikib:
I am trying to send ctrl+k keystroke through serial write to my computer.

What PC program is receiving your input?

Normally serial output won't be detected as keystrokes. You need to use a Leonardo with its special functions. So I suspect that you are sending data to a program that is prepared to take serial input.

...R

Robin2:
What PC program is receiving your input?

Normally serial output won't be detected as keystrokes. You need to use a Leonardo with its special functions. So I suspect that you are sending data to a program that is prepared to take serial input.

...R

Robin2,

The Op is using a utility called ACC keys. ACC Keys

Ray

mrburnette:
The Op is using a utility called ACC keys. ACC Keys

Thanks Ray, I missed that bit.

I presume that program defines the sort of input it can accept.

...R

Thank you MattS-UK,
I will look into your remarks tomorrow. I appreciate everyones help. And provide feedback.

I have now purchased my third Arduino and my first Leonardo as it may be helpful with emulation function though i would like to continue to work with the Uno.

MattS-UK:
This is what you meant to do.

  char KeyCommand = 11;      //an ASCII code is a character type (1 byte)

Serial.write(keyCommand);   //write one character to the serial port




The compiler would also accept this, although it's not what you want to do


char someChars[] = {72, 69, 76, 76, 79, 32, 87, 79, 82, 76, 68};
 Serial.write(someChars, 11);  //write 11 chars to the serial port

I have tried the code you have mention and I am not getting a compiler error any more however the CTRL/K is not working.

Would anyone happen to know the "CTRL" key code?

I am assuming I could use something like this.

char SendKey[] = {CTRL, K}  // The actual codes needed
Serial.write(sendkey, 2)

I need CTRL + K to go at the same time as I am unable to change the hard-coded command in the software. My serial program on the computer is receiving the CTRL/K but it is not the correct command.

tahikib:
Would anyone happen to know the "CTRL" key code?

Maybe this is helpful: General Input Device Emulator

mrburnette:
Maybe this is helpful: General Input Device Emulator

So I took a look at that and it looks like I can send these Escape command to the ACC Keys program. I am going to try when I get a chance. The code compiled just fine but I don't have my laptop with the software needed to check it.

Thank you for the point in that direction. :smiley:

void changeColor () {
  // char KeyCommand = 11;
  char KeyCommand[] = {27,44,'hold',44,'control',46,107,46}; // <esc>,hold,control.k.
  Serial.write(KeyCommand);

UPDATE:

So I have up my speed to 115200 and Serial.write to Serial.println and it has reduced some sending errors. However I am now getting a little extra surprise at the end of the command. I am now using serialmon to check the commands. With ASCII as the read out. The log has it as plain text and you cannot see the command.

When I press the button I get the proper command with extra data at the end. I am basing this off of General Input Device Emulator suggested by mrburnette

2015-01-01 22:19:00.431 R "combine,control.k.5" 04 01 "M" 06 1E 0D 0A

I expect the EOT, ACK, and etc but the 5 after the "k" I am not sure what it is there for.

void changeColor () {
  digitalWrite(LEDPIN, HIGH);
  char KeyCommand[] = {27,44,'c','o','m','b','i','n','e',44,'c','o','n','t','r','o','l',46,107,46};  // Send <esc>,combined,control.k.
  Serial.println(KeyCommand);
  digitalWrite(LEDPIN, LOW);
  delay(100);
}

I don't have the other computer to check if the command work correctly with the software.

Dumped Serial.Write CTLR + K.

Got a Leonardo and the Keyboard. is working great.

CLOSE

1 Like