Skip to content

Commit

Permalink
Merge pull request #156 from kou-yeung/feature/key_event_20240526
Browse files Browse the repository at this point in the history
add key event
  • Loading branch information
kou-yeung authored May 26, 2024
2 parents f1f75fd + f8ed44c commit 5c513d8
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Assets/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions Assets/WebGLSupport/WebGLInput/Events.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 2 additions & 0 deletions Assets/WebGLSupport/WebGLInput/Events/KeyboardEvent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 34 additions & 3 deletions Assets/WebGLSupport/WebGLInput/WebGLInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ internal class WebGLInputPlugin
[DllImport("__Internal")]
public static extern void WebGLInputOnEditEnd(int id, Action<int, string> cb);

[DllImport("__Internal")]
public static extern void WebGLInputOnKeyboardEvent(int id, Action<int, int, string, int, int, int, int> cb);

[DllImport("__Internal")]
public static extern int WebGLInputSelectionStart(int id);

Expand Down Expand Up @@ -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<int, int> cb) { }
Expand All @@ -83,6 +86,7 @@ public static void WebGLInputOnFocus(int id, Action<int> cb) { }
public static void WebGLInputOnBlur(int id, Action<int> cb) { }
public static void WebGLInputOnValueChange(int id, Action<int, string> cb) { }
public static void WebGLInputOnEditEnd(int id, Action<int, string> cb) { }
public static void WebGLInputOnKeyboardEvent(int id, Action<int, int, string, int, int, int, int> 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; }
Expand All @@ -102,6 +106,9 @@ public static void WebGLInputEnableTabText(int id, bool enable) { }

public class WebGLInput : MonoBehaviour, IComparable<WebGLInput>
{
public static event KeyboardEventHandler OnKeyboardDown;
public static event KeyboardEventHandler OnKeyboardUp;

static Dictionary<int, WebGLInput> instances = new Dictionary<int, WebGLInput>();
public static string CanvasId { get; set; }

Expand Down Expand Up @@ -147,7 +154,8 @@ private void Awake()
if (input.EnableMobileSupport)
{
gameObject.AddComponent<WebGLInputMobile>();
} else
}
else
{
// when disable mobile input. disable self!
enabled = false;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -311,6 +321,26 @@ static void OnTab(int id, int value)
WebGLInputTabFocus.OnTab(instances[id], value);
}

[MonoPInvokeCallback(typeof(Action<int, int, string, int, int, int, int>))]
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)
Expand All @@ -325,7 +355,8 @@ void Update()
if (Application.isMobilePlatform)
{
return;
} else
}
else
{
OnSelect();
}
Expand Down
15 changes: 15 additions & 0 deletions Assets/WebGLSupport/WebGLInput/WebGLInput.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5c513d8

Please sign in to comment.