0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【C#】Win32APIで取得した仮想キーコードをKeys列挙型に変換する方法

Posted at

本文

Win32APIで取得した仮想キーコードを、Formアプリで使用するKeys列挙型に変換する方法。
キーコードをKeysにキャストしたものと、ControlクラスのModifierKeysをORすると変換がおこなる。

C#
Keys keys = (Keys)キーコード | Control.ModifierKeys;

一例

キーフックのコールバック関数内で変換する場合。

C#
[StructLayout(LayoutKind.Sequential)]
internal class KBDLLHOOKSTRUCT
{
    public uint vkCode;
    public uint scanCode;
    public uint flags;
    public uint time;
    public UIntPtr dwExtraInfo;
}

private IntPtr HookProcedure(int nCode, IntPtr wParam, KBDLLHOOKSTRUCT lParam)
{
    if (nCode == 0)
    {
        Keys keys = (Keys)lParam.vkCode | Control.ModifierKeys;
    }

    return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
}

参考

Windows FormsのControlクラス
https://github.com/dotnet/winforms/blob/main/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?