9

I want to interact with a Virtualbox-VM to do an unattended install of Virtualbox-Guest-Additions. Therefore I have to send keystrokes to the VM. I'm able to do this by using the VBoxManage-API, where I send the HEX of the scancode to the VM:

# sends "b" to the VM
VBoxManage controlvm "$VMNAME" keyboardputscancode <HEX>

Now I want to send a complete command to the VM, like:

mount /dev/scd0 /media/cdrom0<Enter>

So I have to convert every single char of that string into a scancode and send it to the VM by using VBoxManage.

How do I convert these chars to the corresponding keyboard scancodes on Linux? My VM has the same keyboard mapping like my host. I'm looking for something I can use like that:

$ getscancode n
> 31

?

Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
  • I just found `showkey -s`, which gives me the correct scancode. But that only works in a Terminal outside of my Window Manager, Otherwise I get `Couldn't get a file descriptor referring to the console`. – Wolkenarchitekt Feb 22 '12 at 13:47
  • 2
    `xev` will provide the information you need, but not in such a neat format as `showkey -s` (so I'm hesitant to give it as an answer). You will need to put the cursor into the black box, then type the keys you need. – Edd Steel Feb 22 '12 at 16:35
  • See also [Translate ascii codes to AT set 2 keyboard scancodes](http://unix.stackexchange.com/questions/250377/translate-ascii-codes-to-at-set-2-keyboard-scancodes) – Gilles 'SO- stop being evil' Dec 20 '15 at 01:22

1 Answers1

5

showkey --ascii seems to work for most keys, and it does work in my GNOME Terminal.

Community
  • 1
  • 1
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • 1
    this only works in an interactive mode. I want to get the keychar from within a script, so i can call something like `getscancode n`. – Wolkenarchitekt Mar 19 '12 at 17:35
  • 1
    showkey --ascii does either not report the correct scancodes in my case, or doesn't report them at all, just ascii glyph, octal pseudo scancode and keycode – Don Question Dec 20 '15 at 12:17