-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add paste from clipboard support to FlxInputText. #240
Conversation
I've tested this and it's working well so far. I'd suggest to add functionality for pasting on macOS with the following change:
The Windows key registers as the Command key (Cmd-V) on macOS. |
Good idea! I'll be adding this now. Thank you for the suggestion!! ^^ |
should we detect os and choose a key rather than allowing both keys? |
|
||
// Copy and paste | ||
if ((FlxG.keys.pressed.CONTROL || FlxG.keys.pressed.WINDOWS) && hasFocus) | ||
{ | ||
if (FlxG.keys.justPressed.V) | ||
{ | ||
var clipboard = Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT, ClipboardTransferMode.CLONE_PREFERRED); | ||
if (clipboard != null) | ||
{ | ||
text = text.substr(0, caretIndex - 1) + text.substring(caretIndex, text.length); // removes the "v" | ||
text = text.substring(0, caretIndex - 1) + clipboard + text.substring(caretIndex - 1, text.length); | ||
caretIndex += Std.int(clipboard.length - 1); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage tests are failing because FlxG.keys are only available when #if FLX_KEYBOARD
is defined, meaning that if FLX_NO_KEYBOARD
is defined this change will cause compile errors. Using FlxG.keys here is not a good idea, since it does not honor the order in which keys are pressed in the same frame, which can make a significant difference in typing, especially at lower framerates
Take a look at how keyboard events are handled elsewhere in the class: https://github.com/HaxeFlixel/flixel-ui/blob/dev/flixel/addons/ui/FlxInputText.hx#L332
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right. Yeah, I completely disregarded these. I'll be changing this up soon while taking this information into account
added #250 to switch from keyCode ints to FlxKey enum identifiers for readibility to add ctrl+V to this you would add: case V if (e.ctrlKey):
final clipboardText = Clipboard.generalClipboard.getData(TEXT_FORMAT, CLONE_PREFERRED);
if (clipboardText != null)
{
text = insertSubstring(text, clipboardText, caretIndex);
caretIndex += clipboardText.length;
onChange(INPUT_ACTION);
} ctrlKey will check for ctrl on windows and command on mac, however, when i try this on my mac (on html5), clipboard is null even though I have text copied, I'll make an issue in openfl. Also onKeyDown isn't firing when SPACE is pressed, but this is happening on dev as well |
Ah, clipboard isn't working on html5 because your using Could you look into what the best way to have basic clipboard text reading/writing on all targets? maybe try the openfl forums? I see there's lime.system.Clipboard, but I'm not sure how it's used |
I partially have it working in HTML5 using lime.system.Clipboard. We need to register events within FlxInputText—I'll post some code for it soon. The main problem is that I'm encountering a separate strange bug with HaxeFlixel. Any time I use the keys to paste text, it always works the first time but then subsequent dual key commands no longer work. If I press X or control/command separately I can get text to trace out, but if I press them together they do not register. This problem is only occurring on HTML5 builds. Edit: There's definitely something wrong with using FlxG.keys.pressed.WINDOWS (Command on macOS) on HTML5. When you press it in combination with another key it gets stuck as pressed down. |
This PR can be closed now, fixed by #253. |
While trying to make something, I've noticed that you can't paste text from your clipboard for whatever reason
The more I thought about it, it could really be of help to have such feature added in...
It's not perfect, but it works pretty fine.