0

I write a little client using WinForms, C#, AxMsTscNotSafeForScripting tool, and I need to send Win+R on the VM(yes, I know, there's a way to launch app remotely without RunDialog, but I need to). I quickly found nice lib named InputSimulator, it simulates everything correctly on my main machine, but no effect on VM.

Actually, it can send the whole string into notepad, but when it deals with VirtualKeyCodes, nothing happens. After drilling Google a bit deeper I found usage of WindowsAPI.SendInput with methods void PressKey(char ch, bool press) void KeyDown(ushort scanCode) void KeyUp(ushort scanCode).

PressKey is working with VM, but KeyDown and KeyUp, which I need, are incorrect. For example, 81 is ScanCode for "Q", but it prints "." in notepad, 82 is for "R", but I get "0" and I get nothing at all with 91, that's for Windows key. If I press key with a keyboard, everything works, so problem isn't in KeyPreview, EnableWindowsKey, etc settings Both systems are Win7, Oracle VM VirtualBox.

Even if my way is hopeless, what are another ways to send Win+R programmly to VM? Help will be greatly appreciated!

Amateur
  • 21
  • 1
  • It's not the *length* of your question that's the problem, it's the complete lack of organization. Please edit. You also will want to use backticks (``) around code to make it appear correctly. – Ben Voigt Nov 30 '14 at 23:58
  • @BenVoigt Hope now it looks better, it's my 1st question here, thanks for your advice, now I'd be glad to have some answers if possible – Amateur Dec 01 '14 at 09:51
  • That is *amazingly* better. – Ben Voigt Dec 01 '14 at 15:16

1 Answers1

0

The reason PressKey is working is that you're supplying an ASCII char, which it expects.

The reason KeyDown and KeyUp are not working is that you're still supplying an ASCII value, but these expect a scan code. Scan codes are not the same as ASCII codes. 82 (0x52) is not "R" in any of the common scancode tables -- in both Table 2 and 3 it is NumPad 0, which is consistent with your observed behavior.

You need to use MapVirtualKey(Ex) to translate your ASCII code or virtual key code into a scan code.

It looks like "R" is 0x13 (in both Table 2 and 3), but the WinKey has a different code in every table, so you should not hard-code the value but get it at runtime using MapVirtualKeyEx.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thank you very much! So strange I used VK-table instead of such. However, I still doing something wrong and still no winkey arrived to VM – Amateur Dec 02 '14 at 23:54
  • @Amateur: What did `MapVirtualKeyEx` tell you is the scan code for the Windows key? – Ben Voigt Dec 03 '14 at 00:09
  • @Amateur: `5B` does appear in that table for the Windows key... but as `E0,5B` (an extended key). Try sending either `0xE05B` or sending two keys, `0xE0` then `0x5B`. – Ben Voigt Dec 04 '14 at 23:17