Input -> Keyboard -> "Paste as text (send characters)"

Here you can provide suggestions on how to improve the product, website, etc.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by socratis »

Sorin,
I thought about your proposal as well. It's doable. At least as a proof-of-concept at the beginning. Not advertised, not public, something like one of those "internalcommands" type of things and just from the CLI. For the beginning. The most important is having the mechanism/foundation in place. Afterwards we make it as light or complicated as we're willing to take it.

So, if any brave developer is willing to give us general guidelines on how to transfer clipboard to key-presses, it would be great. I have a vague idea, but it would be better if I have a guiding hand overseeing me. I would then make a draft proposal (so we don't be wandering around), get the OK for the implementation and we continue from there.

Finally, once we green light it, I'd suggest that we move the discussion to the "Developers" section. What does everyone think?
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by scottgus1 »

The only hint I could possibly think of in this suggestion is VBscript's Sendkeys command, which takes a string of characters and sends them as if they're being typed at the keyboard. (Sendkeys apparently has Linux (xdotool?) and Applescript* equivalents, according to a bit of googling). If one could make a script taking the desired text as a command line argument in a guest and launch it using "Vboxmanage guestcontrol" from the host, the guest would think someone is typing into the active window or program in the guest.

*if there were Mac Guest Additions...

A suitable host script could either offer a textbox for receiving pasted text or that could read the clipboard contents then launch the "Vboxmanage guestcontrol start-the-script text-to-be-sent" command.

Just a thought on a way to go, not at all "the" way to do it.
michaln
Oracle Corporation
Posts: 2973
Joined: 19. Dec 2007, 15:45
Primary OS: MS Windows 7
VBox Version: PUEL
Guest OSses: Any and all
Contact:

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by michaln »

The whole (or at least major) point of this exercise is to make it work when there are no Guest Additions.

That said, if you can get that functionality to simulate the input to the VirtualBox GUI on the host, that would achieve the same end result.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by scottgus1 »

FWIW, VBscript Sendkeys does not feed keystrokes into a Virtualbox guest. (just tried it)
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by socratis »

Scott, I think you're thinking too high (on the API sense, not in the recreational sense :) ). Which is also a good plan, albeit a different approach. Not a bad one, I simply haven't thought it through. Or at all to be honest...

What I had in mind is, starting at UIMachineLogic::sltTypeCtrlBreak(), you can see how the "Send XYZ..." keyboard input logic is implemented. It uses "keyboard().PutScancodes", which leads to the function "PutScancodes" in "Main/src-client/KeyboardImpl.cpp". If you continue later on that file, you can see exactly how the "Control-Alt-Delete" trick is achieved.

So, starting from this "key sequence" example and using the keyscan codes from something like http://wiki.osdev.org/PS/2_Keyboard we could be tapping into the clipboard contents (haven't researched this one yet) and send the appropriate codes.

That's pretty much where I'm right now in the flow logic. Not that far...
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
michaln
Oracle Corporation
Posts: 2973
Joined: 19. Dec 2007, 15:45
Primary OS: MS Windows 7
VBox Version: PUEL
Guest OSses: Any and all
Contact:

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by michaln »

Keep in mind that there are 3 more or completely independent parts here. One is taking text (ASCII or whatever) from the clipboard. One is converting text to a sequence of scan codes. And one is injecting a sequence of scan codes into a VM. The last part is mostly there, the first is not but is relatively easy, and the middle is where it gets really nasty.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by socratis »

The three parts of the problem:
  1. michaln wrote:taking text (ASCII or whatever) from the clipboard
    Yep, that's the part I haven't look at yet, but, according to an authority...
    michaln wrote:[this] is relatively easy
    That's what I wanted to hear! And BTW, not whatever, just minimal ASCII.
  2. michaln wrote:converting text to a sequence of scan codes
    michaln wrote:[that's] where it gets really nasty
    More on this later...
  3. michaln wrote:injecting a sequence of scan codes into a VM
    michaln wrote:[that] part is mostly there
    Again, that's what I wanted to hear ;)
Now, about part 2. That's where a lookup table could come in handy, no? You map ASCII to QWERTY key scancodes and you're done. If the clipboard contains anything that can't be mapped, throw an error (and the towel). Pretty much the same as in mapping Ctrl-Down to 0x1d and Ctrl-up to 0x9d. Yes, it would be key-down, key-up for every key, but you'd have to do it only once.

That would be on the first pass, where only a single, QWERTY layout is supported. Later on, if that works, we can expand that, based on some ideas thrown by sorin_postelnicu, i.e. having different lookup tables for different keyboard layouts, and either manually, via the preferences or by auto-identification, figure out which lookup table gets picked for the job, no?
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
michaln
Oracle Corporation
Posts: 2973
Joined: 19. Dec 2007, 15:45
Primary OS: MS Windows 7
VBox Version: PUEL
Guest OSses: Any and all
Contact:

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by michaln »

socratis wrote:Now, about part 2. That's where a lookup table could come in handy, no? You map ASCII to QWERTY key scancodes and you're done. If the clipboard contains anything that can't be mapped, throw an error (and the towel). Pretty much the same as in mapping Ctrl-Down to 0x1d and Ctrl-up to 0x9d. Yes, it would be key-down, key-up for every key, but you'd have to do it only once.
It's not entirely trivial. Sending key-down and key-up codes for each key press is inevitable, that's the way keyboards work. Well, it might be sufficient to send key-up codes in some cases, but I would not recommend that (risks confusing the guest and/or not working properly). But there are things like capitals, where the Shift key needs to be pressed. You could send shift-down, key-down, key-up, shift-up for each letter, but if you have two or more in sequence, it's far better to not toggle the shift key all the time.

And the reason for that is that it's not possible to send the input infinitely fast. The trouble is that there are finite-size buffers and sending input too fast will lose data. That's actually especially a problem with USB keyboards because a typical (physical) USB keyboard cannot send more than 125 events per second in the best case. Which is plenty for a human typist, but not so much if you try to paste a few hundred characters of text. So reducing the number of keystrokes that need to be sent is important. PS/2 keyboards do not have a theoretical speed limit but they do have practical limits as well. Again, sending too much too fast will overrun buffers and lose data. So the simulated input needs to be spaced out a bit (with no feedback!) so that the guest can keep up.

For some characters, relatively complicated keystroke sequences may be needed, especially if you venture away from the US keyboard layout. On Windows, MapVirtualKey or MapVirtualKeyEx might be used to convert from ASCII to scan codes, but I honestly don't know how well those routines work. Those APIs can't be called directly, but could be potentially used to generate translation tables.

Also, instead of PS/2 (XT) scan codes, it would be possible to use USB HID codes. That probably makes little practicl difference because HID <-> PS/2 codes can be translated relatively easily in either direction.

Oh, and before I forget: For a possibly very bad example, look at https://www.virtualbox.org/svn/vbox/tru ... oxshell.py . Examine the function 'typeInGuest' and the subroutines and arrays it uses.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by socratis »

OK, fine. A delay in between "keystrokes" it is then. Actually, it would be really funny/cool if the delay was something like .5 sec and it looked like those haunted terminals in the movies 8)
michaln wrote:complicated keystroke sequences may be needed, especially if you venture away from the US keyboard layout
Not planning to. At the beginning at least. But I plan on planning to include the capability to handle them gracefully at some point later on.
michaln wrote:Also, instead of PS/2 (XT) scan codes, it would be possible to use USB HID codes. That probably makes little practical difference because HID <-> PS/2 codes can be translated relatively easily in either direction.
I'll take a look if I can.
michaln wrote:For a possibly very bad example
Any "good" ones? Is this the "learn from other people's mistakes" teaching method? :)

Since you pointed me to the trunk, I'm trying to get the trunk locally (in a VM actually) so that I can be testing the whole thing. The weird thing is that I get only up to 5.1.4 revisions, not the 5.1.8 that we're today. Any ideas why's that?
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
michaln
Oracle Corporation
Posts: 2973
Joined: 19. Dec 2007, 15:45
Primary OS: MS Windows 7
VBox Version: PUEL
Guest OSses: Any and all
Contact:

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by michaln »

socratis wrote:
michaln wrote:For a possibly very bad example
Any "good" ones?
None that I know of, sorry.
Since you pointed me to the trunk, I'm trying to get the trunk locally (in a VM actually) so that I can be testing the whole thing. The weird thing is that I get only up to 5.1.4 revisions, not the 5.1.8 that we're today. Any ideas why's that?
VirtualBox OSE corresponds to in-developed "next" VirtualBox, not VirtualBox 5.1.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by socratis »

michaln wrote:VirtualBox OSE corresponds to in-developed "next" VirtualBox, not VirtualBox 5.1.
Ah, OK, something like 5.2.x right? So, should I stick with the 5.1.8 source code tarball, or the trunk one? Or maybe both just to play it safe? Since this is the first time I'm going to be building VirtualBox, and I'm trying to follow the building instructions, what's your suggestion?
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
michaln
Oracle Corporation
Posts: 2973
Joined: 19. Dec 2007, 15:45
Primary OS: MS Windows 7
VBox Version: PUEL
Guest OSses: Any and all
Contact:

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by michaln »

socratis wrote:Ah, OK, something like 5.2.x right?
Yes. Whatever the version number will be.
So, should I stick with the 5.1.8 source code tarball, or the trunk one? Or maybe both just to play it safe? Since this is the first time I'm going to be building VirtualBox, and I'm trying to follow the building instructions, what's your suggestion?
I think it's better to go with the OSE source code. Think of it this way, whatever you contribute would go into the next VirtualBox version, not the already released one.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by socratis »

OK, I'll start working on a proof-of-concept proposal. Just to lay out the work that has to be done and we take it from there. Thanks for the info.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
petruchito
Posts: 1
Joined: 23. Feb 2018, 01:42

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by petruchito »

I use AutoHotKey for this, click in the VM window, hit the host key (Left Alt+Right Alt by default) and press Pause.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
#SingleInstance force ; Forbid multiple instances
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

pause::SendRaw %clipboard%
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Input -> Keyboard -> "Paste as text (send characters)"

Post by socratis »

Funny that you woke up this thread, because I was about to...

Well, without too much fanfare, that feature was introduced in VirtualBox 5.2.0. The relevant change (changeset 64906) was made on 2016-12-16, about a couple of months after this discussion fell into a "coma".

There wasn't an announcement in the changelog and I just found it by accident, only yesterday! There are two new sub-commands for the "VBoxManage controlvm" command, besides the old trusty "keyboardputscancode". Below is the only documentation you're going to find for the moment:
VBoxManage controlvm <uuid|vmname>
                     keyboardputscancode <hex> [<hex> ...]
                     keyboardputstring <string1> [<string2> ...]
                     keyboardputfile <filename>
Well, it works as imagined! Almost...

You use the "keyboardputstring" with an ASCII string. Note that it accepts only ACSII strings and not all of them, or at least I haven't found a way to "pass" the <Enter> key. And if you want to send the "\" character you have to escape it, at least on my OSX host and I suspect on Linux as well, but not on Windows. So, in order to send a command like "DIR C:\" you need to issue the commands:
  • VBoxManage controlvm "FreeDOS" keyboardputstring dir c:\\
    VBoxManage controlvm "FreeDOS" keyboardputscancode 1c 9c
Notice the double "\\". Again, not needed in Windows.

Also, note that this is followed by the sub-command "keyboardputscancode" where "1c" is the <Enter> key and "9c" is the <Enter><released> key (0x1c + 0x80, thanks Michaln!). Why is that needed? Because as I said 1) I couldn't find a way to send the <Enter> key and 2) if you don't send the release event, the <Enter> gets stuck! ;)

To achieve the same thing, but with a file, here's a sample command:
  • VBoxManage controlvm "FreeDOS" keyboardputfile "/Users/socratis/Desktop/cmd.txt"
and the contents of the "cmd.txt" file, note that I've added the lines numbers:

Code: Select all

1   cls
2   cd C:\
3   date
4
5   time
6
7   dir
8
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Post Reply