Skip to content
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

Closed
wants to merge 5 commits into from

Conversation

bog4d
Copy link

@bog4d bog4d commented Oct 30, 2022

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.

@bog4d bog4d changed the title Add paste from clipboard support. Add paste from clipboard support to FlxInputText. Oct 30, 2022
@DigiEggz
Copy link
Contributor

DigiEggz commented Aug 1, 2023

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:

// Copy and paste
if ((FlxG.keys.pressed.CONTROL || FlxG.keys.pressed.WINDOWS) && hasFocus)
{ ...

The Windows key registers as the Command key (Cmd-V) on macOS.

@bog4d
Copy link
Author

bog4d commented Aug 1, 2023

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:

// Copy and paste
if ((FlxG.keys.pressed.CONTROL || FlxG.keys.pressed.WINDOWS) && hasFocus)
{ ...

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!! ^^

@Geokureli
Copy link
Member

should we detect os and choose a key rather than allowing both keys?

Comment on lines +326 to +340

// 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);
}
}
}
Copy link
Member

@Geokureli Geokureli Aug 2, 2023

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

Copy link
Author

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

@Geokureli
Copy link
Member

Geokureli commented Aug 2, 2023

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

@Geokureli
Copy link
Member

Geokureli commented Aug 2, 2023

Ah, clipboard isn't working on html5 because your using openfl.desktop.Clipboard the only html5 equiv i can find is: https://api.haxe.org/js/html/Clipboard.html#readText. but this seems to be asyncronous which sounds like a can of worms

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

@DigiEggz
Copy link
Contributor

DigiEggz commented Aug 9, 2023

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.

@DigiEggz
Copy link
Contributor

This PR can be closed now, fixed by #253.

@Geokureli Geokureli closed this Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants