Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to capture the '#' character on different locale keyboards in WPF/C#?

Tags:

c#

wpf

keypress

My WPF application handles keyboard presses and specifically the # and * character as it is a VoIP phone.

I have a bug though with international keyboards, and in particular the British english keyboard. Normally I listen for the 3 key and if the shift key modifier is down we fire off an event to do stuff. However on the British keyboard this is the '£' character. I found that the UK english keyboard has a dedicated key for '#'. Obviously we could just listen for that particular key, but that doesn't solve the case for US english which is shift-3 and all the countless other keyboards that put it somewhere else.

Long story short, how do I listen for a particular character from a key press, whether it's a key combo or single key and react to it?

like image 994
Jippers Avatar asked Apr 28 '11 22:04

Jippers


People also ask

How do I do a screen capture?

Press the Power and Volume down buttons at the same time. If that doesn't work, press and hold the Power button for a few seconds. Then tap Screenshot.

How do you capture a cursor?

To take a screenshot that includes the mouse cursor, check the option labeled “Include Mouse Cursor” in the “Capture Setup” window, and then click “Start” at the bottom of the window. To capture screenshots with IrfanView, use the keyboard shortcut Control+F11.

How do I capture a Snipping Tool?

Press Ctrl + PrtScn keys. The entire screen changes to gray including the open menu. Select Mode, or in earlier versions of Windows, select the arrow next to the New button. Select the kind of snip you want, and then select the area of the screen capture that you want to capture.

How do I capture a screenshot of an area?

Press “Windows + Shift + S”. Your screen will appear grayed out and your mouse cursor will change. Click and drag on your screen to select the part of your screen you want to capture. A screenshot of the screen region you selected will be copied to your clipboard.


2 Answers

The function below, GetCharFromKey(Key key) will do the trick.

It uses a series of win32 calls to decode the key pressed:

  1. get the virtual key from WPF key

  2. get the scan code from the virtual key

  3. get your unicode character

This old post describes it in a bit more detail.

      public enum MapType : uint       {          MAPVK_VK_TO_VSC = 0x0,          MAPVK_VSC_TO_VK = 0x1,          MAPVK_VK_TO_CHAR = 0x2,          MAPVK_VSC_TO_VK_EX = 0x3,       }        [DllImport("user32.dll")]       public static extern int ToUnicode(           uint wVirtKey,           uint wScanCode,           byte[] lpKeyState,           [Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)]              StringBuilder pwszBuff,           int cchBuff,           uint wFlags);        [DllImport("user32.dll")]       public static extern bool GetKeyboardState(byte[] lpKeyState);        [DllImport("user32.dll")]       public static extern uint MapVirtualKey(uint uCode, MapType uMapType);        public static char GetCharFromKey(Key key)       {          char ch = ' ';           int virtualKey = KeyInterop.VirtualKeyFromKey(key);          byte[] keyboardState = new byte[256];          GetKeyboardState(keyboardState);           uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);          StringBuilder stringBuilder = new StringBuilder(2);           int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);          switch (result)          {             case -1:                 break;             case 0:                 break;             case 1:                {                   ch = stringBuilder[0];                   break;                }             default:                {                   ch = stringBuilder[0];                   break;                }          }          return ch;       } 
like image 99
George Avatar answered Oct 07 '22 16:10

George


I found a helpful solution in this post: http://www.codewrecks.com/blog/index.php/2008/05/06/wpf-convert-systemwindowsinputkey-to-char-or-string-2/

There is another event TextInput or PreviewTextInput which gives the character as string instead of the Key :).

like image 45
JanDotNet Avatar answered Oct 07 '22 17:10

JanDotNet