Trouble with sending input via RDP by all known ways - c#

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!

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.

Related

Printing a sharp sign character

I am trying to print this Unicode character(and many others) but the Console displays a ?.
It should print the sharp sign https://unicode-table.com/en/266F/
Could you please help me?
"kjfnv" is in UTF-16BE, I have tried it in UTF-16LE too (6F 26), but it does not work
char kjfnv = '\u6F26';
I am trying to print this Unicode character(and many others) but the Console displays a '?'.
The problem is not with the character, but it's the console not being configured the correct font to display this character.
You can manually set a console's Font, here's a tutorial on how to do it. I don't know of a way to set it programmatically (yet).
https://www.tenforums.com/tutorials/93961-change-console-window-font-font-size-windows.html
Here's a .net Fiddle: https://dotnetfiddle.net/xLvRq2
using System;
public class Program
{
public static void Main()
{
char kjfnv = '\u266F';
Console.WriteLine(kjfnv);
}
}
Output:
♯
Update:
my personal set of applicable fonts in the console is very limited - meaning: I am not able to display the character.
However, the .net fiddle proves the methodology is correct - meaning: changing your console host, or find a way to make use of the correct font in Console should do the trick.
Here's a article on how to change fonts in powershell. Possibly a similar thing exists for the default console host: https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/adding-more-fonts-to-powershell-console
#EricMovsessian: wasn't able to get this working without seriously needing to change some default values. For me this is not worth further investigation.
Keep in mind Console graphic capabilities are very limited, perhaps you want to consider an alternative technique, or just use the number sign # as a substitute

How to Type Multilingual Characters in Windows Mobile Device by Device Keypad

I have a smart device with windows Mobile 6.5 operating system, and i want to write an application for this device by .Net CF 3.5 and C# Language. The keybpad of this device is something look like this picture:
As you see in the top picture, texts and numbers are placed on the same key. For example 2 has “ABC” if we wanted to write anything starting with ‘A’ we need to type key 2 once. If we wanted to type ‘B’, press key 2 twice and thrice for typing ‘C’.
Q1. I can not type English letters using this keyboard, I just can type digits. I want to type English letters in text boxes (And to Persian letters). Can I do this job by keybd_event() that is an extern function ?
Q2. I have 3 method for input characters and at the same time one of these methods must be enabled :
Type digits
Type English letters (with some characters like . , ? / )
Type Persian Letters (with some characters like . , ? / )
In other hands in How to switch between the three methods
Q3. how to show keyboard by appropriate characters according to one of 3 input methods that described? (One time with English, another time with Persian, and another time with digits)
Regards
If the special key interpretation is only needed inside one application (Compact Framework) then possibly using SDF (SmartDeviceFramework) is the coice. See KeyTest3AKsdf on http://www.hjgode.de/wp/2012/09/20/windows-mobile-cf-how-to-catch-f1-and-f2-in-weh/
In the Message Handler you need to manage the keyboard plane (numbers, english letters, persian letters) and probably a key-pressed timeout to be able to decide if a key is pressed multiple times within a time (to produce these ABC optional outputs).
Define a key or combination to switch between the planes.
Watch the messages coming in and decide what to do. You can alter a copy of the message, post it to the message queue and return true to let windows system know that you handled the message.
You may use keybd_event for simple keys as defined in winuser.h and winuserm.h. But for extended chars this will get complicated (finding the right sequence of values). I would go with PostMessage and WM_CHAR for extended symbols and letters.
Further on you may need to install a trueype font with persian glyphs. The default english OS fimrware normally only support a subset of unicode glyphs. See also http://www.hjgode.de/wp/2011/04/06/mobile-development-a-simple-unicode-character-map/

Trap NULL key in WPF application

I am using a barcode scanner and we were looking for a way to determine if input was from a keyboard or the scanner. The input will be used in a WPF POS application. We thought we had come up with a good method by using the scanners ability to prefix reads with custom characters. The first character ideally would be non printable so we chose NULL or '\0'. We used the following code to register an event handler
EventManager.RegisterClassHandler(typeof(System.Windows.Controls.Control), System.Windows.Controls.Control.KeyUpEvent, new RoutedEventHandler(KeyUpEvent));
internal static void KeyUpEvent (object sender, RoutedEventArgs e)
{
KeyEventArgs keyEvent = e as KeyEventArgs;
if (keyEvent != null)
{
keyEvent.Key.ToString();
}
}
This however seems to not get the first NULL character and instead moves to the next char which is the length of the data.
I have also tested with a console app and just done a Console.ReadKey(). This returns me the null char as the first thing read so I know the scanner is definitely sending the correct data.
So is there any way in WPF to obtain all the data read?
Edit:
I tried using TextCompositionEventHandler but to no avail, i still only get the printable characters coming through.
KeyEventArgs.Key is not a character, it's an enum indicating which key was pressed. There is no NULL keyboard key so there is no point trying to check for the ASCII NULL (0x00) character. Moreover, most non-printable characters have no equivalent key and require the user to use a combination of keys to type them.
If you want to detect scanner codes using prefixes, try chekcing the UIElement.TextInput TextBoxBase.TextChanged events.
A better idea may be to use the scanner's SDK (if available) to send data directly to your application. More expensive models usually have an SDK to communicate with applications directly instead of emulating the keyboard.
I might be wrong, but I don't think NULL and \0 is the same in this context. Have a look at this post. They suggest using TextInput instead of KeyUp.
How to get KeyChar in WPF application

Converting KBDLLHOOKSTRUCT(.NET) to KeyEvent/Char(Java), JNA

So, basically what i'm doing is using JNA to set a LowLevelKeyboardProc Keyboard hook, everything works perfectly fine, i can get the values exactly like i want them in java, but the problem i get is when trying to convert to chars, it becomes extremely ennoying handling caps locks, SHIFT keys and tons of other things like everything thats not a-z 0-9 on the keyboard, i was wondering if there is a easier way to do the conversion?
heres the details of what I'm getting from the hook every time a key is pressed
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644967(v=VS.85).aspx
, i Figured it might be best to find a way to manually generate a KeyEvent(Not char, since i need something to handle things like F keys, caps lock button, CTRL button etc etc).
Any help i can get is highly appriciated!.
The Abbot project (http://abbot.sf.net) has a system for mapping keycodes to keychars, using predefined keyboard mappings (it generates a wide variety of keystrokes and records the resulting character output). However, Java does not provide a way for "predicting" the resulting character output given a particular key code.
There may be something within the MS libraries.

WM_KEYDOWN : how to use it?

I'm trying to send a key stroke to one application, through PostMessage. I am using too Spy++ to try to understand how to send the message, as I do not fully understand its inner workings.
In this picture, the first item(selected item) was made with an actual key stroke made by myself. The one with a red elipse around it(below) was made with the following code:
WinApi.PostMessage(InsideLobbyHandle, WinApi.WM_KEYDOWN, (int)WinApi.VK_UP, 1);
I guess it must have something to do with the last PostMessage() parameter, but I can't figure out how it really works. I can see in the original key stroke the ScanCode = 48, and in mine its 0, and also fExtended is 1 and in mine is 0. How can I make it look the same?
In http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx I cannot understand the last parameter's working.
Simulate keyboard input using SendInput, not PostMessage.
You can't simulate keyboard input with PostMessage.
There are still some caveats with respect to keyboard state/async-state:
The SendInput function does not reset
the keyboard's current state.
Therefore, if the user has any keys
pressed when you call this function,
they might interfere with the events
that this function generates. If you
are concerned about possible
interference, check the keyboard's
state with the GetAsyncKeyState
function and correct as necessary.
The lParam for the WM_KEYDOWN Notification is specified in terms of the bits of the field:
The first 16 bits are the repeat count
The next 8 bits are the scan code
The next bit is 1 for extended key, 0 otherwise
The next 4 bits are reserved and must be 0
The next bit is always 0 (for WM_KEYDOWN)
The next bit is the previous key state
The last bit is always 0 (for WM_KEYDOWN)
A warning: Any solution you build based around PostMessage is going to be very brittle.
Take a look at http://inputsimulator.codeplex.com, it wraps the SendInput method mentioned by Kevin
In Spy++ if you right click on the highlighted (logged message) entry and look at its properties, You can see the exact value of the lParam. You can then use that as your lParam to ensure that the PostMessage leads to similar effects, as the manual action did.

Categories

Resources