How do I paste text into a text input sprite at the point of the cursor.?

0 favourites
  • 7 posts
From the Asset Store
Connect the dots in the correct order and draw happy animals!
  • How do I paste text into a text input object at the point of the cursor.?

    I want to paste "Hello World" into a text input sprite at this example location:

    Text input has "This is a test"... I want to paste in "Hello World" here...

    "This is a >paste here | This is the current cursor position< test" - the result would be:

    "This is a Hello World test"

    This is a dynamic modification so I cant simply amend as a simple replacement.

    In other words - the paste-in text could be many different text blocks.

    Thank You - d

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • As a follow-up, currently I am using "amend text" but it always places at the bottom, not where the cursor is positioned.

  • I’m guessing you’re using the textInput object? First step would be to get the cursor position. If construct doesn’t provide an expression for that you can use js to get that since textInput is an html dom element.

    Anyways you’d then use the len,left and right expressions to insert text.

    Set text to left(textInput.text, pos)&"inserted"&right(textInput.text, len(textInput.text)-pos)

  • No luck, but this is my latest attempt (as stated, I have the text on the clipboard):

    function pasteAtCursor() {
     // Get a reference to the text input object
     const input = runtime.objects.myTextInput.GetInputControl();
    
     // Get the current cursor position
     const startPos = input.selectionStart;
     const endPos = input.selectionEnd;
    
     // Read the text from the clipboard
     navigator.clipboard.readText().then((clipboardText) => {
     // Insert the clipboard text at the current cursor position
     const currentValue = input.value;
     const newValue = currentValue.substring(0, startPos) + clipboardText + currentValue.substring(endPos);
     input.value = newValue;
    
     // Update the cursor position to be immediately after the pasted text
     input.setSelectionRange(startPos + clipboardText.length, startPos + clipboardText.length);
     });
    }
    
  • I guess look at your use of substring. Just curious why this is needed, you can already paste with ctrl+v or right click and select paste.

  • The Why: I am having the user click buttons - the action then pastes a text string into the place where the cursor is placed (within the text-input object. Currently the "amend action" always places it at the end.

    The app calls for the user to choose where to paste the string - they do not know what the string is and it is dynamic so therefore it can't be hardcoded. Make sense?

    If there is an easier way, I'm all ears :-)

  • So what doesn’t that code you have not do? Any errors show up in the browsers console?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)