How to convert from Virtual Key codes to System.Windows.Forms.Keys

17,316

Solution 1

The integer values for System.Windows.Forms.Keys enum match that of the Win32 calls.

Keys keyData = (Keys)rawWin32KeyCode;

Solution 2

Use KeyInterop.KeyFromVirtualKey().

Share:
17,316
Jeremy
Author by

Jeremy

Software Developer

Updated on June 05, 2022

Comments

  • Jeremy
    Jeremy almost 2 years

    If I intercept a key press using win32 calls, I now have a key code. Is there a way to convert that to a System.Windows.Forms.Keys value?

  • xcud
    xcud about 15 years
    If the int and char keycode are interchangeable then this previous solution should work: <a href="stackoverflow.com/questions/544141/…>
  • whale70
    whale70 almost 6 years
    The question was about WinForms; KeyInterop is a WPF class not available under WinForms
  • Jens Bornschein
    Jens Bornschein almost 6 years
    This is not working if some modifier keys (shift, ctrl., alt) are pressed! lParam.vkCode = 160 // = VK_LSHIFT lParam.scanCode = 42 Keys key = (Keys)lParam.vkCode; key = Space | F17