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

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.

Related

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

Keys enumeration C#, AltKey

The three modifier keys are Control, Alt and Shift.
In the keys enumeration, there are Control and ControlKey, Shift and ShiftKey and Alt, but AltKey is missing.
Keys.ControlKey refers to the actual Ctrl key while Keys.Control refers to the control modification.
Similarly,Keys.ShiftKey refers to the actual Shift key while Keys.Shift refers to the shift modification.
What about the actual Alt key?
It can be pressed by itself and the corresponding element in the Keys enumeration should be Keys.AltKey!
Can you please explain the lack of the vlaue Keys.AltKey in the Keys enumeration?
Actually, the real key (not modifier) enumeration value for Alt key is Keys.Menu with the value of 18. That's just a naming convention. They decided to use the Alt name just for the modifer, and Menu for the non-modifier. Interestingly, the documentation for the Keys.Menu says "The ALT key". We also have Keys.LMenu and Keys.RMenu.
At a guess I'd say that the lack of the Alt key is due to the fact that in WinForms development, the paradigm used for setting shortcut keys is to prefix a letter with the & character, e.g., E&xit. When the application is run, this is automatically set up as a shortcut command Alt+X. Most windows shortcut keys are set up this way in development pretty much removing any reason why you'd want to detect it in code - or at least a KeyDown event. That said, I personally would have expected the Alt key to be available.

Having an issue with XNA and Keyboard.GetState() or any other Keyboard problems?

I am having trouble with Keyboard.GetState() and .IsKeysDown(). They simply won't work.
If you have Synergy, then it might be one of the problems.
I believe it clears the keyboard input cache or something along the lines, so when you poll the hardware again Windows doesn't have any keystrokes to identify.
So when debugging in XNA, disable Synergy.
It sounds like you may be experiencing a couple of issues. Since other new developers are likely to find this question when looking for help with Keyboard input I will try to cover both.
1) Keyboard.GetState() returns a snapshot of which keys are currently being held down. KeyboardState.IsKeyDown() does not identify keystrokes. It just tells you which keys are currently being pressed. Due to the way this works, it is best if you store the keyboard state before attempting to use it.
2) In your Update loop you will want to store the current keyboard state:
KeyboardState newKeyboardState = Keyboard.GetState();
Now you can check if a key press has occurred. A key press means that a key used to be down, and is now up:
if(previousKeyboardState.IsKeyDown(Keys.A && !newKeyboardState.IsKeyDown(Keys.A)) {
Console.Out.WriteLine("Keystroke: A");
}
Finally, save the current state so you can check against it later:
previousKeyboardState = newKeyboardState;

Capture Key Sequence via ProcessCmdKey

I override ProcessCmdKey in my application and can get any single keypress with modifiers (eg. Alt+Ctrl+X). What I want to do is mimic the short cut handling of say ReSharper where the user holds down the control key and then R, M to open the refactor dialog
I have found plenty of references to capture key plus modifier combinations but not much for the sequence. There is this Capture multiple key downs in C# but it uses the KeyDown Event.
There are also key mining examples such as this http://www.codeproject.com/KB/system/simple_key_log.aspx that capture everything and use native calls.
Am I able to extend my ProcessCmdKey to handle the key sequences or do I need to look elsewhere? Since I have a large number of shortcuts captured in ProcessCmdKey I would rather not have to start again if possible
Thanks
In order to achieve the functionality you want you simply need to keep track of the sequence of KeyPress events.
You can create a class to keep track of the last key combination that was pressed in ProcessCmdKey. If that particular combination does not match a mapped command but it is the first element of a sequence you can store it in your class. Then the next time ProcessCmdKey is activated check your new KeyPressTracker class to determine if a sequence has been started. If it has then check if the newly pressed key combination is the second element of one you specify. Please see the pseudocode example below:
Step 1: ProcessCmdKey is activated. The key combination is Ctrl+R, this does not match a command that you want to process but it is the first element of a sequence that you want to use (Ctrl+R+M).
Step 2: Store this key-press in a new class you created to keep track of the last key-press.
KeyPressTracker.Store(KeyCode, Modifiers);
Step 3: ProcessCmdKey is activated a second time. This time, the key combination is Ctrl+M which is not a key-press we're looking for but is the second element of a sequence. We check the last stored keypress using the new KeyPressTracker class. This will allow you to match a "sequence" such as Ctrl+R and Ctrl+M.
var lastKeyPress = KeyPressTracker.GetLastKeyPress();
if (lastKeyPress == "Ctrl+R" && currentKeyPress == "Ctrl+M")
{
// Show Refactor dialog
}

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.

Categories

Resources