Skip to content

Commit

Permalink
keydown / up / press support
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Jan 29, 2024
1 parent 94c6ae2 commit 5e7cd45
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
19 changes: 19 additions & 0 deletions haxe/ui/backend/ComponentImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import haxe.ui.core.ImageDisplay;
import haxe.ui.core.Screen;
import haxe.ui.core.TextDisplay;
import haxe.ui.core.TextInput;
import haxe.ui.events.KeyboardEvent;
import haxe.ui.events.MouseEvent;
import haxe.ui.events.UIEvent;
import haxe.ui.geom.Rectangle;
Expand Down Expand Up @@ -371,6 +372,24 @@ class ComponentImpl extends ComponentBase {
_eventMap.set(MouseEvent.MIDDLE_MOUSE_UP, listener);
}
}

case KeyboardEvent.KEY_DOWN:
if (hasTextInput() && !_eventMap.exists(KeyboardEvent.KEY_DOWN)) {
_eventMap.set(KeyboardEvent.KEY_DOWN, listener);
getTextInput().onKeyDown = listener;
}

case KeyboardEvent.KEY_UP:
if (hasTextInput() && !_eventMap.exists(KeyboardEvent.KEY_UP)) {
_eventMap.set(KeyboardEvent.KEY_UP, listener);
getTextInput().onKeyUp = listener;
}

case KeyboardEvent.KEY_PRESS:
if (hasTextInput() && !_eventMap.exists(KeyboardEvent.KEY_PRESS)) {
_eventMap.set(KeyboardEvent.KEY_PRESS, listener);
getTextInput().onKeyPress = listener;
}
}
}

Expand Down
96 changes: 94 additions & 2 deletions haxe/ui/backend/TextInputImpl.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package haxe.ui.backend;

import haxe.ui.events.FocusEvent;
import haxe.ui.core.InteractiveComponent;
import h2d.TextInput;
import haxe.ui.core.InteractiveComponent;
import haxe.ui.events.FocusEvent;
import haxe.ui.events.KeyboardEvent;
import haxe.ui.events.UIEvent;
import hxd.Event;
import hxd.Key;

class TextInputImpl extends TextDisplayImpl {

Expand Down Expand Up @@ -85,4 +88,93 @@ class TextInputImpl extends TextDisplayImpl {
return measureTextRequired;
}

private var _onKeyDown:KeyboardEvent->Void = null;
public var onKeyDown(null, set):KeyboardEvent->Void;
private function set_onKeyDown(value:KeyboardEvent->Void):KeyboardEvent->Void {
_onKeyDown = value;
if (_onKeyDown == null && _onKeyUp == null && _onKeyPress == null) {
unregisterInernalEvents();
return value;
}
registerInternalEvents();
return value;
}

private var _onKeyUp:KeyboardEvent->Void = null;
public var onKeyUp(null, set):KeyboardEvent->Void;
private function set_onKeyUp(value:KeyboardEvent->Void):KeyboardEvent->Void {
_onKeyUp = value;
if (_onKeyDown == null && _onKeyUp == null && _onKeyPress == null) {
unregisterInernalEvents();
return value;
}
registerInternalEvents();
return value;
}

private var _onKeyPress:KeyboardEvent->Void = null;
public var onKeyPress(null, set):KeyboardEvent->Void;
private function set_onKeyPress(value:KeyboardEvent->Void):KeyboardEvent->Void {
_onKeyPress = value;
if (_onKeyDown == null && _onKeyUp == null && _onKeyPress == null) {
unregisterInernalEvents();
return value;
}
registerInternalEvents();
return value;
}

private var _internalEventsRegistered = false;
private function registerInternalEvents() {
if (_internalEventsRegistered) {
return;
}
_internalEventsRegistered = true;
textInput.onKeyDown = onKeyDownInternal;
textInput.onKeyUp = onKeyUpInternal;
}

// heaps doesnt have a keypress event, so we'll hold onto down keys in order to dispatch the press event
private var _downKeys:Map<Int, Bool> = new Map<Int, Bool>();
private function unregisterInernalEvents() {
textInput.onKeyDown = null;
textInput.onKeyUp = null;
_internalEventsRegistered = false;
}

private function onKeyDownInternal(e:Event) {
_downKeys.set(e.keyCode, true);
dispatchEvent(KeyboardEvent.KEY_DOWN, e.keyCode);
}

private function onKeyUpInternal(e:Event) {
var hadDownKey = (_downKeys.exists(e.keyCode) && _downKeys.get(e.keyCode) == true);
_downKeys.remove(e.keyCode);
dispatchEvent(KeyboardEvent.KEY_UP, e.keyCode);
if (hadDownKey) {
dispatchEvent(KeyboardEvent.KEY_PRESS, e.keyCode);
}
}

private function dispatchEvent(type:String, keyCode:Int) {
var event = new KeyboardEvent(type);
event.keyCode = keyCode;
event.altKey = Key.isDown(Key.ALT);
event.shiftKey = Key.isDown(Key.SHIFT);
event.ctrlKey = Key.isDown(Key.CTRL);
switch (type) {
case KeyboardEvent.KEY_DOWN:
if (_onKeyDown != null) {
_onKeyDown(event);
}
case KeyboardEvent.KEY_UP:
if (_onKeyUp != null) {
_onKeyUp(event);
}
case KeyboardEvent.KEY_PRESS:
if (_onKeyPress != null) {
_onKeyPress(event);
}
}
}
}

0 comments on commit 5e7cd45

Please sign in to comment.