There seems to be some changes in the Windows 8 consumer preview with regards to KeyEventArgs. What is the best way to interpret KeyEventArgs and tell what key was pressed or released? I am guessing I need to map the Key (of type VirtualKey) property to the actualy ascii code (I am happy with ascii and not worry about unicode and other keyboard layout). Is there an easier way to manage shift/control and other combined keystrokes?
Does KeyboardEvent work for you?
http://msdn.microsoft.com/en-us/library/windows/apps/hh465793.aspx
UPDATE:
This is only for Javascript.
Leaving this there in case any future ones is looking for this.
Related
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.
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
I have been searching around for an answer to this but I can't seem to find anything. Does anyone know if you can determine the letter casing in Keys?
For example:
if (System.Windows.Forms.Keys.A.ToString() == "A")
{
// Upper or Lower?
}
Thanks.
There is no casing, it represents a physical key on your keyboard. Do you see an 'a' and an 'A' on your keyboard?
You can check and see if a Shift key is depressed.
System.Windows.Forms.Keys.A represents the physical key A on your keyboard. It does not have a case. Thus, your question does not make sense.
If you want to check whether the user holds the Shift key on the keybord, there's also System.Windows.Forms.Keys.Shift.
There is no simple mapping between keys and characters. Keyboard layouts can work differently. One example are dead keys. And once you get to IMEs it gets even more complicated. Do not try to duplicate a keyboard layout manually in your application.
If you want to get what character a user entered, handle WM_CHAR, not WM_KEY_DOWN/UP. It's exposed as Control.KeyPress event in winforms.
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.
I am thinking of making a few video turorials on C#, my problem is that I don't type very fast and I don't want to put the user to sleep as they watch me typing in real-time.
I would like to write a small C# program that will take a line of text and feed it to the keyboard buffer, so that I can simulate keyboard typing.
Does anyone know how to access the keboard buffer to do this?
If this has been done before or if someone knows of an existing program to do this, can you point me in the right direction.
Thanks.
You should use SendKeys, i can show you how to use
here is an example: SendKeys.Send('A');
but u can use it with your own character: SendKeys.Send(CHARACTER HERE);
what happens if we have a string variable, you will get nothing
if it happens use it this way:
string letter = "exampleletter";
foreach (char ch in letter)
SendKeys.Send(ch.ToString());
i hope it works for you
Yogibear
At PDC and other conferences I've been at, they make liberal use of code snippets to quickly drop the new code into place.
I'm not really sure that you can write to keyboardbuffer or something like this
what I know it to send to some windows some keyboard commands
in your case it will be sending keyboard commands to notepad probably
in that case use the function provided above
but I would recommend to cut video parts (typing moments) out of your video
instead of writing code in C#
You could use
SendKeys Class
Provides methods for sending keystrokes to an application.