# Restricting certain keys in text input

**URL:** <https://discourse.psychopy.org/t/restricting-certain-keys-in-text-input/12364>\
**Category:** Builder\
**Created:** [April 14, 2020, 4:55pm UTC](https://discourse.psychopy.org/t/restricting-certain-keys-in-text-input/12364 "2020-04-14T16:55:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Borg](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/nicholas_borg/32/7740_2.png) [@Nicholas\_Borg](https://discourse.psychopy.org/u/Nicholas_Borg)\
**Post date:** [April 14, 2020, 4:55pm UTC](https://discourse.psychopy.org/t/restricting-certain-keys-in-text-input/12364/1 "2020-04-14T16:55:37Z")

</div>

Hello,

I have a task where the participant is required to type a response to a question.

To allow text input, I have copied the code from a recommended text input experiment ( [https://gitlab.pavlovia.org/demos/textinput/](https://gitlab.pavlovia.org/demos/textinput/). The JS code is as follows:

```auto
let theseKeys = psychoJS.eventManager.getKeys();
if (theseKeys.length > 0) { // at least one key was pressed
  textAdd = theseKeys[theseKeys.length-1]; 
  }

if (textAdd === 'return') {
    textAdd = ''; // Add nothing
    continueRoutine = false; // End routine (if that is what you want)
} else if (textAdd === 'space') {
    textAdd = ' '; // Add a space
} else if (textAdd === 'period') {
    textAdd = '.'; // Add a period
} else if (textAdd === 'backspace') {
    text.text = text.text.slice(0, -1);
    textAdd = undefined;
} else if (['lshift', 'rshift'].includes(textAdd)) {
    modify = true;
} else if (textAdd !== undefined) {
    if (modify) {
        text.text = text.text + textAdd.toUpperCase();
        modify = false;
    } else {
        text.text = text.text + textAdd
    }
    textAdd = undefined;
}

```

If I understand correctly, the .getKeys() command summons all available keys on the keyboard. Pressing the period button prints the word “period”, but I have corrected this using: `} else if (textAdd === 'period') { textAdd = '.'; // Add a period`

However, keys such as “num\_1”, “backslash”, “equals”, etc are also being printed when I don’t really need them. I only need the alphabet, the numbers (not numpad) and some punctuation. Would I need to replicate the same process for every other key or is there a way I can restrict these keys from being pressed/registered?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [April 14, 2020, 6:41pm UTC](https://discourse.psychopy.org/t/restricting-certain-keys-in-text-input/12364/2 "2020-04-14T18:41:17Z")

</div>

Hi @Nicholas_Borg , the getKeys method takes a `keyList` argument, allowing you to define which keys should be checked for. E.g., use:

```javascript
let theseKeys = psychoJS.eventManager.getKeys({keyList: ['a', 'b', 'c']})
// This example will only accept keys 'a', 'b', and 'c'

```

---

<div class="post-metadata">

**Author:** ![Nicholas\_Borg](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/nicholas_borg/32/7740_2.png) [@Nicholas\_Borg](https://discourse.psychopy.org/u/Nicholas_Borg)\
**Post date:** [April 15, 2020, 8:58am UTC](https://discourse.psychopy.org/t/restricting-certain-keys-in-text-input/12364/3 "2020-04-15T08:58:32Z")

</div>

Thank you @dvbridges, that worked perfectly. Will check out the documentation a bit better next time 🙂
