From f8ed44cfe947eade0f6ec338545ba7f1f1c3e354 Mon Sep 17 00:00:00 2001 From: kou_yeung Date: Sun, 26 May 2024 12:41:13 +0900 Subject: [PATCH] add key event --- Assets/Sample.cs | 13 +++++++ Assets/WebGLSupport/WebGLInput/Events.meta | 8 ++++ .../WebGLInput/Events/KeyboardEvent.cs | 20 ++++++++++ .../WebGLInput/Events/KeyboardEvent.cs.meta | 2 + Assets/WebGLSupport/WebGLInput/WebGLInput.cs | 37 +++++++++++++++++-- .../WebGLSupport/WebGLInput/WebGLInput.jslib | 15 ++++++++ 6 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 Assets/WebGLSupport/WebGLInput/Events.meta create mode 100644 Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs create mode 100644 Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs.meta diff --git a/Assets/Sample.cs b/Assets/Sample.cs index 4a835cf..02dfeb0 100644 --- a/Assets/Sample.cs +++ b/Assets/Sample.cs @@ -14,6 +14,19 @@ public void Start() { v.AddManipulator(new WebGLInputManipulator()); }); + + WebGLSupport.WebGLInput.OnKeyboardDown += OnKeyboardDown; + WebGLSupport.WebGLInput.OnKeyboardUp += OnKeyboardUp; + } + + private void OnKeyboardUp(WebGLSupport.WebGLInput input, KeyboardEvent keyboardEvent) + { + Debug.Log(string.Format("Sample:OnKeyboardUp({0}) shift({1}) ctrl({2}) alt({3})", keyboardEvent.Key, keyboardEvent.ShiftKey, keyboardEvent.CtrlKey, keyboardEvent.AltKey)); + } + + private void OnKeyboardDown(WebGLSupport.WebGLInput input, KeyboardEvent keyboardEvent) + { + Debug.Log(string.Format("Sample:OnKeyboardDown({0}) shift({1}) ctrl({2}) alt({3})", keyboardEvent.Key, keyboardEvent.ShiftKey, keyboardEvent.CtrlKey, keyboardEvent.AltKey)); } public void OnValueChange(InputField o) diff --git a/Assets/WebGLSupport/WebGLInput/Events.meta b/Assets/WebGLSupport/WebGLInput/Events.meta new file mode 100644 index 0000000..35f0957 --- /dev/null +++ b/Assets/WebGLSupport/WebGLInput/Events.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 863f75c59ad9cf44b89143346c17e55b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs b/Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs new file mode 100644 index 0000000..cec5357 --- /dev/null +++ b/Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs @@ -0,0 +1,20 @@ +namespace WebGLSupport +{ + public delegate void KeyboardEventHandler(WebGLInput input, KeyboardEvent keyboardEvent); + public sealed class KeyboardEvent + { + public string Key { get; } + public int Code { get; } + public bool ShiftKey { get; } + public bool CtrlKey { get; } + public bool AltKey { get; } + public KeyboardEvent(string key, int code, bool shiftKey, bool ctrlKey, bool altKey) + { + Key = key; + Code = code; + ShiftKey = shiftKey; + CtrlKey = ctrlKey; + AltKey = altKey; + } + } +} diff --git a/Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs.meta b/Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs.meta new file mode 100644 index 0000000..826cc51 --- /dev/null +++ b/Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 81551fe61b23b9d41b0a261e062d1ba0 \ No newline at end of file diff --git a/Assets/WebGLSupport/WebGLInput/WebGLInput.cs b/Assets/WebGLSupport/WebGLInput/WebGLInput.cs index 7149bdf..b7c92c1 100644 --- a/Assets/WebGLSupport/WebGLInput/WebGLInput.cs +++ b/Assets/WebGLSupport/WebGLInput/WebGLInput.cs @@ -42,6 +42,9 @@ internal class WebGLInputPlugin [DllImport("__Internal")] public static extern void WebGLInputOnEditEnd(int id, Action cb); + [DllImport("__Internal")] + public static extern void WebGLInputOnKeyboardEvent(int id, Action cb); + [DllImport("__Internal")] public static extern int WebGLInputSelectionStart(int id); @@ -74,7 +77,7 @@ internal class WebGLInputPlugin public static extern void WebGLInputEnableTabText(int id, bool enable); #endif #else - public static void WebGLInputInit() {} + public static void WebGLInputInit() { } public static int WebGLInputCreate(string canvasId, int x, int y, int width, int height, int fontsize, string text, string placeholder, bool isMultiLine, bool isPassword, bool isHidden, bool isMobile) { return 0; } public static void WebGLInputEnterSubmit(int id, bool flag) { } public static void WebGLInputTab(int id, Action cb) { } @@ -83,6 +86,7 @@ public static void WebGLInputOnFocus(int id, Action cb) { } public static void WebGLInputOnBlur(int id, Action cb) { } public static void WebGLInputOnValueChange(int id, Action cb) { } public static void WebGLInputOnEditEnd(int id, Action cb) { } + public static void WebGLInputOnKeyboardEvent(int id, Action cb) { } public static int WebGLInputSelectionStart(int id) { return 0; } public static int WebGLInputSelectionEnd(int id) { return 0; } public static int WebGLInputSelectionDirection(int id) { return 0; } @@ -102,6 +106,9 @@ public static void WebGLInputEnableTabText(int id, bool enable) { } public class WebGLInput : MonoBehaviour, IComparable { + public static event KeyboardEventHandler OnKeyboardDown; + public static event KeyboardEventHandler OnKeyboardUp; + static Dictionary instances = new Dictionary(); public static string CanvasId { get; set; } @@ -147,7 +154,8 @@ private void Awake() if (input.EnableMobileSupport) { gameObject.AddComponent(); - } else + } + else { // when disable mobile input. disable self! enabled = false; @@ -199,7 +207,9 @@ public void OnSelect() WebGLInputPlugin.WebGLInputOnBlur(id, OnBlur); WebGLInputPlugin.WebGLInputOnValueChange(id, OnValueChange); WebGLInputPlugin.WebGLInputOnEditEnd(id, OnEditEnd); + WebGLInputPlugin.WebGLInputOnKeyboardEvent(id, OnKeyboardEvent); WebGLInputPlugin.WebGLInputTab(id, OnTab); + // default value : https://www.w3schools.com/tags/att_input_maxlength.asp WebGLInputPlugin.WebGLInputMaxLength(id, (input.characterLimit > 0) ? input.characterLimit : 524288); WebGLInputPlugin.WebGLInputFocus(id); @@ -311,6 +321,26 @@ static void OnTab(int id, int value) WebGLInputTabFocus.OnTab(instances[id], value); } + [MonoPInvokeCallback(typeof(Action))] + static void OnKeyboardEvent(int id, int mode, string key, int code, int shiftKey, int ctrlKey, int altKey) + { + if (!instances.ContainsKey(id)) return; + var instance = instances[id]; + + // mode : keydown(1) keyup(3) + var cb = mode switch + { + 1 => OnKeyboardDown, + 2 => OnKeyboardUp, + _ => default + }; + + if (cb != null) + { + cb(instance, new KeyboardEvent(key, code, shiftKey != 0, ctrlKey != 0, altKey != 0)); + } + } + void Update() { if (input == null || !input.isFocused) @@ -325,7 +355,8 @@ void Update() if (Application.isMobilePlatform) { return; - } else + } + else { OnSelect(); } diff --git a/Assets/WebGLSupport/WebGLInput/WebGLInput.jslib b/Assets/WebGLSupport/WebGLInput/WebGLInput.jslib index 877eea8..83a696d 100644 --- a/Assets/WebGLSupport/WebGLInput/WebGLInput.jslib +++ b/Assets/WebGLSupport/WebGLInput/WebGLInput.jslib @@ -156,6 +156,21 @@ var WebGLInput = { (!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, buffer]) : {{{ makeDynCall("vii", "cb") }}}(id, buffer); }; }, + WebGLInputOnKeyboardEvent:function(id, cb){ + var input = instances[id]; + var func = function(mode, e) { + var bufferSize = lengthBytesUTF8(e.key) + 1; + var key = _malloc(bufferSize); + stringToUTF8(e.key, key, bufferSize); + var code = e.code; + var shift = e.shiftKey ? 1 : 0; + var ctrl = e.ctrlKey ? 1 : 0; + var alt = e.altKey ? 1 : 0; + (!!Runtime.dynCall) ? Runtime.dynCall("viiiiiii", cb, [id, mode, key, code, shift, ctrl, alt]) : {{{ makeDynCall("viiiiiii", "cb") }}}(id, mode, key, code, shift, ctrl, alt); + } + input.addEventListener('keydown', function(e) { func(1, e); }); + input.addEventListener('keyup', function(e) { func(2, e); }); + }, WebGLInputSelectionStart:function(id){ var input = instances[id]; return input.selectionStart;