Created
July 28, 2018 17:28
-
-
Save sh-akira/c29355f74c2c66f63fe9a240e28fbe1b to your computer and use it in GitHub Desktop.
Unity Keyboard Event Sample
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using System.Threading.Tasks; | |
public class KeyboardEventArgs : EventArgs | |
{ | |
public int KeyCode { get; } | |
public string KeyName { get; } | |
public KeyboardEventArgs(int keyCode) : base() | |
{ | |
KeyCode = keyCode; KeyName = KeyboardAction.KeyCodeString[keyCode]; | |
} | |
} | |
public static class KeyboardAction | |
{ | |
public static EventHandler<KeyboardEventArgs> KeyDownEvent; | |
public static EventHandler<KeyboardEventArgs> KeyUpEvent; | |
[DllImport("user32.dll", SetLastError = true)] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
private static extern bool GetKeyboardState(byte[] lpKeyState); | |
private static bool IsInitialized = false; | |
private static bool[] LastKeys = new bool[256]; | |
public static void Update() | |
{ | |
int i; | |
var keys = new bool[256]; | |
var states = new byte[256]; | |
if (GetKeyboardState(states)) | |
{ | |
for (i = 0; i < 256; i++) | |
{ | |
keys[i] = (states[i] & 0x80) == 0x80; | |
} | |
} | |
if (IsInitialized) | |
{ | |
for (i = 0; i < 256; i++) | |
{ | |
if (keys[i] != LastKeys[i]) | |
{ | |
if (keys[i]) KeyDownEvent(keys, new KeyboardEventArgs(i)); | |
else KeyUpEvent(keys, new KeyboardEventArgs(i)); | |
} | |
} | |
} | |
else | |
{ | |
IsInitialized = true; | |
} | |
LastKeys = keys; | |
} | |
public static string[] KeyCodeString = new string[] { | |
"", | |
"左クリック", | |
"右クリック", | |
"コントロールブレイク", | |
"中クリック", | |
"マウス第一拡張", | |
"マウス第二拡張", | |
"未定義", | |
"BackSpace", | |
"Tab", | |
"予約済", | |
"予約済", | |
"Clear", | |
"Enter", | |
"未定義", | |
"未定義", | |
"Shift", | |
"Ctrl", | |
"Alt", | |
"Pause", | |
"CapsLock", | |
"IME かな", | |
"未定義", | |
"IME Junja", | |
"IME ファイナル", | |
"IME 漢字", | |
"未定義", | |
"Esc", | |
"IME 変換", | |
"IME 無変換", | |
"IME 使用可能", | |
"IME モード変更要求", | |
"スペース", | |
"Page Up", | |
"Page Down", | |
"End", | |
"Home", | |
"←", | |
"↑", | |
"→", | |
"↓", | |
"Select", | |
"Print", | |
"Execute", | |
"PrintScreen", | |
"Insert", | |
"Delete", | |
"Help", | |
"0", | |
"1", | |
"2", | |
"3", | |
"4", | |
"5", | |
"6", | |
"7", | |
"8", | |
"9", | |
"未定義", | |
"未定義", | |
"未定義", | |
"未定義", | |
"未定義", | |
"未定義", | |
"未定義", | |
"A", | |
"B", | |
"C", | |
"D", | |
"E", | |
"F", | |
"G", | |
"H", | |
"I", | |
"J", | |
"K", | |
"L", | |
"M", | |
"N", | |
"O", | |
"P", | |
"Q", | |
"R", | |
"S", | |
"T", | |
"U", | |
"V", | |
"W", | |
"X", | |
"Y", | |
"Z", | |
"左Windows", | |
"右Windows", | |
"アプリケーション", | |
"予約済", | |
"Sleep", | |
"テンキー0", | |
"テンキー1", | |
"テンキー2", | |
"テンキー3", | |
"テンキー4", | |
"テンキー5", | |
"テンキー6", | |
"テンキー7", | |
"テンキー8", | |
"テンキー9", | |
"テンキー*", | |
"テンキー+", | |
"区切り記号", | |
"テンキー-", | |
"テンキー.", | |
"テンキー/", | |
"F1", | |
"F2", | |
"F3", | |
"F4", | |
"F5", | |
"F6", | |
"F7", | |
"F8", | |
"F9", | |
"F10", | |
"F11", | |
"F12", | |
"F13", | |
"F14", | |
"F15", | |
"F16", | |
"F17", | |
"F18", | |
"F19", | |
"F20", | |
"F21", | |
"F22", | |
"F23", | |
"F24", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"NumLock", | |
"ScrollLock", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"未割当", | |
"左Shift", | |
"右Shift", | |
"左Ctrl", | |
"右Ctrl", | |
"左Alt", | |
"右Alt", | |
"ブラウザー戻る", | |
"ブラウザー進む", | |
"ブラウザー更新", | |
"ブラウザー停止", | |
"ブラウザー検索", | |
"ブラウザーお気に入り", | |
"ブラウザー開始/ホーム", | |
"音量ミュート", | |
"音量ダウン", | |
"音量アップ", | |
"次のトラック", | |
"前のトラック", | |
"メディア停止", | |
"メディア再生/一時停止", | |
"メール", | |
"メディア選択", | |
"アプリケーション1", | |
"アプリケーション2", | |
"予約済", | |
"予約済", | |
"[:*]", | |
"[;+]", | |
"[,<]", | |
"[-=]", | |
"[.>]", | |
"[/?]", | |
"[@`]", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"予約済", | |
"未割当", | |
"未割当", | |
"未割当", | |
"[[{]", | |
"[\\|]", | |
"[]}]", | |
"[^~]", | |
"OEM8", | |
"予約済", | |
"OEM固有", | |
"[\_]", | |
"OEM固有", | |
"OEM固有", | |
"IME PROCESS", | |
"OEM固有", | |
"仮想キー下位ワード", | |
"未割当", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"英数", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"OEM固有", | |
"Attn", | |
"CrSel", | |
"ExSel", | |
"Erase EOF", | |
"Play", | |
"Zoom", | |
"予約済", | |
"PA1", | |
"Clear" | |
}; | |
} |
This file contains hidden or 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
using UnityEngine; | |
public class KeyboardController : MonoBehaviour | |
{ | |
// Use this for initialization | |
void Start() | |
{ | |
KeyboardAction.KeyDownEvent += KeyboardAction_KeyDown; | |
KeyboardAction.KeyUpEvent += KeyboardAction_KeyUp; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
KeyboardAction.Update(); | |
} | |
private void KeyboardAction_KeyDown(object sender, KeyboardEventArgs e) | |
{ | |
Debug.Log($"KeyDown:{e.KeyName}"); | |
} | |
private void KeyboardAction_KeyUp(object sender, KeyboardEventArgs e) | |
{ | |
Debug.Log($"KeyUp:{e.KeyName}"); | |
} | |
private void OnApplicationQuit() | |
{ | |
KeyboardAction.KeyDownEvent -= KeyboardAction_KeyDown; | |
KeyboardAction.KeyUpEvent -= KeyboardAction_KeyUp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment