Web target keycodes [resolved]

sign up sign in

I started playing with snow/luxe and found that my key handling code was not working when deploying to web target. I started digging around and found that the keycode handling code was not doing what i expected.

Below is the debug code i wrote to check why the keycodes i recieve in the key event handlers doens't match the Key constants.

import snow.Log.log;
import snow.types.Types;

@:log_as('app')
class Main extends snow.App {
    override function ready() {

        log('Left key: '+ Key.left);
        log('Right key: '+ Key.right);
        log('Up key: '+ Key.up);
        log('Down key: '+ Key.down);

    } //ready

    override function onkeyup( keycode:Int, _,_, mod:ModState, _,_ ) {
        log('key pressed: '+keycode);
    } //onkeyup
} //Main

After running the code i observed the following:

KeyConstantKeycode
Left10737419041104
Right10737419031103
Up10737419061106
Down10737419051105

I then looked at the key constants code and how the key events are handled, and found the following function in InputSystem.hx:

function browser_keycode_to_sdl_key( _code:Int ) : Int

This funtion is supposed to translate the browser keycodes to the values used internally in snow. But the output doesn't match the values used in the constants.

Let's take the left key as example and look what the code does with it:

case 37: return 80 | 1<<10; // left arrow

the value returned for it is: 1104 when the constant is 1073741904.

So i then looked at how the Key constants are computed, and they come from the following:

typedef Key = snow.input.Keycodes.Keycodes;

So i checked the Keycodes class and found that the left key constant is computed as follows:

public static var left : Int = from_scan(Scancodes.left);

Where Scancodes.left values is:

public static var left : Int = 80;

And from_scan function code is:

/** Convert a scancode to a keycode for comparison */
public static function from_scan( scancode : Int ) : Int {
    return (scancode | Scancodes.MASK);
} //from_scan

Where Scancodes.MASK values is:

public static var MASK:Int = (1<<30);

So it computes left key constant as: 80 | 1 << 30 which is 1073741904.

The way it computes the key constants is very similar to the translation it performs with the browser key event.

Constant: 80 | 1 << 30
Key translation: 80 | 1 << 10

After this findings i modified the translation function (browser_keycode_to_sdl_key) to perform the same operations as the Key constatns, and after that the key handling code worked as expected with the web target.

I don't know if that changes have some other non desired side effects, so i will appreciate any feedback.

Tags
snowresolvedwebkeycodes