fzhangtj
6 年前
当前提交
dda01af9
共有 8 个文件被更改,包括 544 次插入 和 499 次删除
-
1Runtime/editor/editor_window.cs
-
490Runtime/rendering/editable.cs
-
26Runtime/service/keyboard.cs
-
52Runtime/service/text_input.cs
-
228Runtime/widgets/editable_text.cs
-
82Runtime/service/raw_keyboard.cs
-
11Runtime/service/text_input_utils.cs.meta
-
153Runtime/service/text_input_utils.cs
|
|||
using System.Collections.Generic; |
|||
using Unity.UIWidgets.foundation; |
|||
using UnityEngine; |
|||
|
|||
namespace Unity.UIWidgets.service { |
|||
|
|||
public class RawKeyboard { |
|||
public static readonly RawKeyboard instance = new RawKeyboard(); |
|||
|
|||
RawKeyboard() { |
|||
|
|||
} |
|||
|
|||
readonly List<ValueChanged<RawKeyEvent>> _listeners = new List<ValueChanged<RawKeyEvent>>(); |
|||
|
|||
public void addListener(ValueChanged<RawKeyEvent> listener) { |
|||
this._listeners.Add(listener); |
|||
} |
|||
|
|||
public void removeListener(ValueChanged<RawKeyEvent> listener) { |
|||
this._listeners.Remove(listener); |
|||
} |
|||
|
|||
internal void _handleKeyEvent(Event evt) { |
|||
if (this._listeners.isEmpty()) { |
|||
return; |
|||
} |
|||
|
|||
var keyboardEvent = RawKeyEvent.fromEvent(evt); |
|||
if (keyboardEvent == null) { |
|||
return; |
|||
} |
|||
|
|||
foreach (var listener in new List<ValueChanged<RawKeyEvent>>(this._listeners)) { |
|||
if (this._listeners.Contains(listener)) { |
|||
listener(keyboardEvent); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public abstract class RawKeyEvent { |
|||
|
|||
protected RawKeyEvent(RawKeyEventData data) { |
|||
this.data = data; |
|||
} |
|||
|
|||
public static RawKeyEvent fromEvent(Event evt) { |
|||
if (evt == null) { |
|||
return null; |
|||
} |
|||
|
|||
if (evt.type == EventType.KeyDown) { |
|||
return new RawKeyDownEvent(new RawKeyEventData(evt)); |
|||
} else if (evt.type == EventType.KeyUp) { |
|||
return new RawKeyUpEvent(new RawKeyEventData(evt)); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
public readonly RawKeyEventData data; |
|||
} |
|||
|
|||
public class RawKeyDownEvent: RawKeyEvent { |
|||
public RawKeyDownEvent(RawKeyEventData data) : base(data) { |
|||
} |
|||
} |
|||
|
|||
public class RawKeyUpEvent : RawKeyEvent { |
|||
public RawKeyUpEvent(RawKeyEventData data) : base(data) { |
|||
} |
|||
} |
|||
|
|||
public class RawKeyEventData { |
|||
public readonly Event unityEvent; |
|||
|
|||
public RawKeyEventData(Event unityEvent) { |
|||
this.unityEvent = unityEvent; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2288dc63570aa4d8db1bd6673b12634f |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
namespace Unity.UIWidgets.service { |
|||
static partial class TextInputUtils { |
|||
static Dictionary<Event, TextInputAction> _keyToOperations; |
|||
|
|||
public static TextInputAction? getInputAction(Event evt) { |
|||
if (_keyToOperations == null) { |
|||
initKeyToOperations(); |
|||
} |
|||
|
|||
EventModifiers m = evt.modifiers; |
|||
evt.modifiers &= ~EventModifiers.CapsLock; |
|||
TextInputAction result; |
|||
var exists = _keyToOperations.TryGetValue(evt, out result); |
|||
evt.modifiers = m; |
|||
if (exists) { |
|||
return result; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
public static void initKeyToOperations() { |
|||
if (_keyToOperations != null) { |
|||
return; |
|||
} |
|||
|
|||
_keyToOperations = new Dictionary<Event, TextInputAction>(); |
|||
|
|||
// key mappings shared by the platforms
|
|||
mapKey("return", TextInputAction.newline); |
|||
mapKey("left", TextInputAction.moveLeft); |
|||
mapKey("right", TextInputAction.moveRight); |
|||
mapKey("up", TextInputAction.moveUp); |
|||
mapKey("down", TextInputAction.moveDown); |
|||
|
|||
mapKey("#left", TextInputAction.selectLeft); |
|||
mapKey("#right", TextInputAction.selectRight); |
|||
mapKey("#up", TextInputAction.selectUp); |
|||
mapKey("#down", TextInputAction.selectDown); |
|||
|
|||
mapKey("delete", TextInputAction.delete); |
|||
mapKey("backspace", TextInputAction.backspace); |
|||
mapKey("#backspace", TextInputAction.backspace); |
|||
|
|||
// OSX is the special case for input shortcuts
|
|||
if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX) { |
|||
// Keyboard mappings for mac
|
|||
// TODO mapKey ("home", TextInputAction.scrollStart);
|
|||
// TODO mapKey ("end", TextInputAction.scrollEnd);
|
|||
// TODO mapKey ("page up", TextInputAction.scrollPageUp);
|
|||
// TODO mapKey ("page down", TextInputAction.scrollPageDown);
|
|||
|
|||
mapKey("^left", TextInputAction.moveGraphicalLineStart); |
|||
mapKey("^right", TextInputAction.moveGraphicalLineEnd); |
|||
// TODO mapKey ("^up", TextInputAction.scrollPageUp);
|
|||
// TODO mapKey ("^down", TextInputAction.scrollPageDown);
|
|||
|
|||
mapKey("&left", TextInputAction.moveWordLeft); |
|||
mapKey("&right", TextInputAction.moveWordRight); |
|||
mapKey("&up", TextInputAction.moveParagraphBackward); |
|||
mapKey("&down", TextInputAction.moveParagraphForward); |
|||
|
|||
mapKey("%left", TextInputAction.moveGraphicalLineStart); |
|||
mapKey("%right", TextInputAction.moveGraphicalLineEnd); |
|||
mapKey("%up", TextInputAction.moveTextStart); |
|||
mapKey("%down", TextInputAction.moveTextEnd); |
|||
|
|||
mapKey("#home", TextInputAction.selectTextStart); |
|||
mapKey("#end", TextInputAction.selectTextEnd); |
|||
// TODO mapKey ("#page up", TextInputAction.selectPageUp);
|
|||
// TODO mapKey ("#page down", TextInputAction.selectPageDown);
|
|||
|
|||
mapKey("#^left", TextInputAction.expandSelectGraphicalLineStart); |
|||
mapKey("#^right", TextInputAction.expandSelectGraphicalLineEnd); |
|||
mapKey("#^up", TextInputAction.selectParagraphBackward); |
|||
mapKey("#^down", TextInputAction.selectParagraphForward); |
|||
|
|||
mapKey("#&left", TextInputAction.selectWordLeft); |
|||
mapKey("#&right", TextInputAction.selectWordRight); |
|||
mapKey("#&up", TextInputAction.selectParagraphBackward); |
|||
mapKey("#&down", TextInputAction.selectParagraphForward); |
|||
|
|||
mapKey("#%left", TextInputAction.expandSelectGraphicalLineStart); |
|||
mapKey("#%right", TextInputAction.expandSelectGraphicalLineEnd); |
|||
mapKey("#%up", TextInputAction.selectTextStart); |
|||
mapKey("#%down", TextInputAction.selectTextEnd); |
|||
|
|||
mapKey("%a", TextInputAction.selectAll); |
|||
mapKey("%x", TextInputAction.cut); |
|||
mapKey("%c", TextInputAction.copy); |
|||
mapKey("%v", TextInputAction.paste); |
|||
|
|||
// emacs-like keybindings
|
|||
mapKey("^d", TextInputAction.delete); |
|||
mapKey("^h", TextInputAction.backspace); |
|||
mapKey("^b", TextInputAction.moveLeft); |
|||
mapKey("^f", TextInputAction.moveRight); |
|||
mapKey("^a", TextInputAction.moveLineStart); |
|||
mapKey("^e", TextInputAction.moveLineEnd); |
|||
|
|||
mapKey("&delete", TextInputAction.deleteWordForward); |
|||
mapKey("&backspace", TextInputAction.deleteWordBack); |
|||
mapKey("%backspace", TextInputAction.deleteLineBack); |
|||
} |
|||
else { |
|||
// Windows/Linux keymappings
|
|||
mapKey("home", TextInputAction.moveGraphicalLineStart); |
|||
mapKey("end", TextInputAction.moveGraphicalLineEnd); |
|||
// TODO mapKey ("page up", TextInputAction.movePageUp);
|
|||
// TODO mapKey ("page down", TextInputAction.movePageDown);
|
|||
|
|||
mapKey("%left", TextInputAction.moveWordLeft); |
|||
mapKey("%right", TextInputAction.moveWordRight); |
|||
mapKey("%up", TextInputAction.moveParagraphBackward); |
|||
mapKey("%down", TextInputAction.moveParagraphForward); |
|||
|
|||
mapKey("^left", TextInputAction.moveToEndOfPreviousWord); |
|||
mapKey("^right", TextInputAction.moveToStartOfNextWord); |
|||
mapKey("^up", TextInputAction.moveParagraphBackward); |
|||
mapKey("^down", TextInputAction.moveParagraphForward); |
|||
|
|||
mapKey("#^left", TextInputAction.selectToEndOfPreviousWord); |
|||
mapKey("#^right", TextInputAction.selectToStartOfNextWord); |
|||
mapKey("#^up", TextInputAction.selectParagraphBackward); |
|||
mapKey("#^down", TextInputAction.selectParagraphForward); |
|||
|
|||
mapKey("#home", TextInputAction.selectGraphicalLineStart); |
|||
mapKey("#end", TextInputAction.selectGraphicalLineEnd); |
|||
// TODO mapKey ("#page up", TextInputAction.selectPageUp);
|
|||
// TODO mapKey ("#page down", TextInputAction.selectPageDown);
|
|||
|
|||
mapKey("^delete", TextInputAction.deleteWordForward); |
|||
mapKey("^backspace", TextInputAction.deleteWordBack); |
|||
mapKey("%backspace", TextInputAction.deleteLineBack); |
|||
|
|||
mapKey("^a", TextInputAction.selectAll); |
|||
mapKey("^x", TextInputAction.cut); |
|||
mapKey("^c", TextInputAction.copy); |
|||
mapKey("^v", TextInputAction.paste); |
|||
mapKey("#delete", TextInputAction.cut); |
|||
mapKey("^insert", TextInputAction.copy); |
|||
mapKey("#insert", TextInputAction.paste); |
|||
} |
|||
} |
|||
|
|||
static void mapKey(string key, TextInputAction action) { |
|||
_keyToOperations[Event.KeyboardEvent(key)] = action; |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue