Tristan Heintzelman
Tristan Heintzelman

Reputation: 13

Change input text to a symbol

I want to start out by saying that I do not know Java Script. I work for a travel agency and I work in content creation & design. I have done minor script work using Sabre Scribe in Eclipse. If people have recommendations on where to go to learn the basics of this, that'd be great. Edit: This is using program Adobe Captivate Execute Javascript Function.

Anyways, my goal right now is change a key-press into a symbol. I need to turn "'" into "‡". I am more than happy to put in the time to learn and research this, but all the research I have done has not got me any closer to finding a solution. I have, however, learned how to create an alert when that key is pressed, I just can't seem to get that key to type a different one.

Long story short, I would appreciate some resources or help in the matter.

Upvotes: 0

Views: 1179

Answers (3)

dougtesting.net
dougtesting.net

Reputation: 571

This code will change the ' character to the symbol when you press the key...

<!doctype html>
<html>
    <head>
        <script
            src="https://code.jquery.com/jquery-3.2.1.min.js"
            integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
            crossorigin="anonymous"></script>
    </head>
    <body>
        <input id="textField" type="text"></input>
        <script>
            jQuery(document).ready(function($) {
                $('#textField').on('keydown', function(e) {
                    //console.log(e.keyCode);   // Will tell you the code for the key pressed.
                    if (e.keyCode == 222) {
                        e.target.value += '‡';   // Add the symbol
                        e.preventDefault();   // Stop the single quote from being added.
                    }
                });
            });
        </script>
    </body>
</html>

Upvotes: 1

dougtesting.net
dougtesting.net

Reputation: 571

Adobe Captivate seems to have its own unique flavour of Javascript. After reading the common-js-interface page, something like this may work - can you try it as a test (I don't have the program)??

window.cpAPIEventEmitter.addEventListener("CPAPI_VARIABLEVALUECHANGED", function(e) {
    var value = window.cpAPIInterface.getVariableValue("myTextBox");
    value += 'testing';
    window.cpAPIInterface.setVariableValue('myTextBox', value);
}, 'myTextBox');

Of course change all places it says 'myTextBox' to the name of your text field variable.

If this works then I can probably figure out some code to change the ' to that symbol.

Upvotes: 1

jhpratt
jhpratt

Reputation: 7130

This response is invalid for reasons stated in the comments. I'm leaving this up for the comment history. It could very well be of help to others as well.

You'll want to do a search and replace (using regex) on the input's onkeyup. You can then swap out the appropriate values using a hash, and replace the value back into the input.

Here's some code I wrote around a week ago that does just that:

// note: this needs to be escaped *here*
const hotSwapVals = {
    ':music:': '♫',
    ':rocket:': '🚀',
    ':sat:': '🛰',
    ':satellite:': '🛰'
};

// swap out text with emoji on an input
function hotSwap(obj) {
    if(obj.constructor !== HTMLInputElement)
        throw 'Object must be HTMLInputElement';

    // must be escaped prior, otherwise errors will occur
    const regex = new RegExp(Object.keys(hotSwapVals).join('|'), 'g');

    const val = obj.value.replace(regex, key => hotSwapVals[key]);
    if(obj.value != val)  // prevents moving cursor to end if not needed
        obj.value = val;
}

This is then bound to the object by passing the input object as a parameter.

Upvotes: 1

Related Questions