Trap NULL key in WPF application - c#

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

Related

Trouble with sending input via RDP by all known ways

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.

how to interpret keyeventargs in windows8 consumer preview

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.

System.Windows.Forms.Keys - Lower or Uppercase?

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.

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.

Read barcode when program doesn't have focus?

I have a form which I read data to textbox from a barcode reader.
and there are some codded barcodes like this
W12346S1 is first step of a work
W12346S2 is second step of a work
W12346S3 is third step of a work
...
U123 is a user he read his code to make process
M456 is a machine user do the work on this machine.
so I want to write data to true textboxes from firs char (W, U, M) in form_KeyDown() event or one different.
(
true textboxes mean if user read a barcode which start with W key let the program write the barcode data to "work tekxtbox" or if he read abarcode which start with U program will write the barcode data to user textbox etc...
)
I wanna make this let the codes select its own textboxes. what is the way?
note: if I use textbox1.Text += e.KeyData.ToString();
the output is : ShiftKey, ShiftW, ShiftD1D2D3D4D6ShiftKey, ShiftS, ShiftD2 W12346S2
for W12346S2
Can't you just read in the text and have something like this:
string FirstChar = BarcodeString.Substring(0,1);
if (FirstChar.Equals("W"))
WorkTextBox.Text = BarcodeString;
if (FirstChar.Equals("U"))
UserTextBox.Text = BarcodeString;
Can input from your barcode reader be distinguished from typed keystrokes? If so, I would recommend that incoming barcodes not be handled by the keystroke handler, but instead use their own special handler which will wait until it has scanned an entire barcodes and then stick it in an appropriate box.
If the input from your reader looks like keystrokes, things are apt to be a little more tricky. You may want to intercept all keystrokes going to your form, look at each keystroke, determine whether it looks like it might be part of a barcode, and buffer it if so. Any time you determine that the buffered data isn't part of a barcode, either because of following characters or because a timer expires, fire your own keystroke events to re-issue the keystrokes. Ensuring that all keystrokes are handled in order may be a little tricky, but hopefully not too bad. It will probably be easier to prevent keystrokes from the barcode reader from going into an inappropriate field, than to provide a good user experience after they do.

Categories

Resources