Loose the keyboard – responding with touch, mouse, or any event

Home Forums General Loose the keyboard – responding with touch, mouse, or any event

This topic contains 1 reply, has 1 voice, and was last updated by  Simon Jackson 3 years, 4 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #8908

    Simon Jackson

    Hi all,

    Thought I’d share a little win I had today – getting touch events to work with QRTEngine. The method should apply to any event (including mouse). I’m not sure what the side effects are (e.g., reduced performance/RT accuracy), so would welcome any comments about this or possible improvements. Nonetheless, here we go…

    I was making a simple reaction time task where a person must respond whenever a stimulus appears on the screen. Using a keyboard, this is done by pressing the spacebar. However, my clients want to use tablets (iPads to be specific). What to do? As far as I’m aware QRTEngine doesn’t natively support touch events, so I looked for a workaround. Looking through the v18 code, I saw that when a question loads, QRTE listens for a keypress event from the document, and checks the charCode of the event. So all I had to do was trigger such an event.

    The JS for my stimulus now looks like this:

    The QRTE.Stimulus.onShowFn is adding a touchstart listener to the document body (whole screen in this case), which will call touch2key(). This means that touch2key(" ") will be called when someone touches the screen. This passes the space key (” “), which is then used to create a keypress event that is dispatched on the document. Voila!

    In onShowFn, you can make adjustments by adding event listeners to elements other than the document (e.g., different buttons etc), and using events other than touchstart (e.g., mousedown, click). Leave the touch2key() function as is, and just make sure that you pass it correct key.

    I hope this is useful for someone, and let me know what you think!

    #8909

    Simon Jackson

    I just noticed that quotation marks were replaced in above code. If using above code, replace " or ' with a quotation mark (“).

Viewing 2 posts - 1 through 2 (of 2 total)

The forum ‘General’ is closed to new topics and replies.