Last active
November 27, 2021 07:12
-
-
Save kusa-mochi/f1da8b06a17d9a8cad7cdb28ea1b8e8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// キーボード操作のイベントが発生したら実行されるメソッド | |
private static IntPtr KeyboardInputCallback (int nCode, IntPtr wParam, IntPtr lParam) { | |
if (nCode < 0) { | |
// キーボードのイベントに紐付けられた次のメソッドを実行する。メソッドがなければ処理終了。 | |
return NativeMethods.CallNextHookEx (_keyboardHookId, nCode, wParam, lParam); | |
} | |
// キーコードを取得する。 | |
KBDLLHOOKSTRUCT param = Marshal.PtrToStructure<KBDLLHOOKSTRUCT>(lParam); | |
switch ((NativeMethods.KeyboardMessage) wParam) { | |
case NativeMethods.KeyboardMessage.WM_KEYDOWN: | |
// キーが押し下げられたときに実行したい処理をここに書く。 | |
// キーコードはparam.vkCode(int型)で得られる。 | |
// System.Windows.Forms.Keys列挙体にキャストして利用もできる。 | |
break; | |
case NativeMethods.KeyboardMessage.WM_KEYUP: | |
// キーが押し上げられたときに実行したい処理をここに書く。 | |
// キーコードはparam.vkCode(int型)で得られる。 | |
// System.Windows.Forms.Keys列挙体にキャストして利用もできる。 | |
break; | |
default: | |
break; | |
} | |
// キーボードのイベントに紐付けられた次のメソッドを実行する。メソッドがなければ処理終了。 | |
return NativeMethods.CallNextHookEx (_keyboardHookId, nCode, wParam, lParam); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment