Search code examples
javascriptnode.jscharemulationkeypress

How to get make code from char in JavaScript?


I am working on v86 emulator for node from this library. I need to send some keyboard inputs, but I first need convert it to make code. Is there any JavaScript function to do it?

a => 0x1E
b => 0x30
c => 0x2E

Here is the table of that codes:

Key      Make  Break        Key    Make  Break

    Backspace     0E    8E          F1  3B    BB
    Caps Lock     3A    BA          F2  3C    BC
    Enter         1C    9C          F3  3D    BD
    Esc       01    81          F4  3E    BE
    Left Alt      38    B8          F7  41    C1
    Left Ctrl     1D    9D          F5  3F    BF
    Left Shift    2A    AA          F6  40    C0
    Num Lock      45    C5          F8  42    C2
    Right Shift   36    B6          F9  43    C3
    Scroll Lock   46    C6          F10 44    C4
    Space         39    B9          F11 57    D7
    Sys Req (AT)  54    D4          F12 58    D8
    Tab       0F    8F

            Keypad Keys            Make   Break

            Keypad 0  (Ins)     52  D2
            Keypad 1  (End)         4F  CF
            Keypad 2  (Down arrow)  50  D0
            Keypad 3  (PgDn)        51  D1
            Keypad 4  (Left arrow)  4B  CB
            Keypad 5            4C  CC
            Keypad 6  (Right arrow) 4D  CD
            Keypad 7  (Home)        47  C7
            Keypad 8  (Up arrow)    48  C8
            Keypad 9  (PgUp)        49  C9
            Keypad .  (Del)         53  D3
            Keypad *  (PrtSc)       37  B7
            Keypad -            4A  CA
            Keypad +            4E  CE

           Key    Make  Break          Key    Make  Break

        A      1E    9E         N      31    B1
        B      30    B0         O      18    98
        C      2E    AE         P      19    99
        D      20    A0         Q      10    90
        E      12    92         R      13    93
        F      21    A1         S      1F    9F
        G      22    A2         T      14    94
        H      23    A3         U      16    96
        I      17    97         V      2F    AF
        J      24    A4         W      11    91
        K      25    A5         X      2D    AD
        L      26    A6         Y      15    95
        M      32    B2         Z      2C    AC

           Key    Make  Break          Key    Make  Break

        1      02    82         -      0C    8C
        2      03    83         =      0D    8D
        3      04    84         [      1A    9A
        4      05    85         ]      1B    9B
        5      06    86         ;      27    A7
        6      07    87         '      28    A8
        7      08    88         `      29    A9
        8      09    89         \      2B    AB
        9      0A    8A         ,      33    B3
        0      0B    8B         .      34    B4
                        /      35    B5

And yes. I can rewrite it to JavaScript, but there is also some other keyboard keys and it will take long time to do it, so this is reason why I am asking this.


Solution

  • All of the answers and comments here completely miss the point. There is no Unicode code for Right Shift press and there is no Unicode code for Right Shift release. Likewise, you cannot make a string out of a Right Shift press and Print Screen release to parse it and print the codes. How could you even possibly put a Right Shift release in a string? Apparently some people here have bigger imagination than me.

    The make and break codes have nothing to do with characters that can be printed on the screen or stored in strings. Those are codes that the keyboard sends to signal key presses and releases. Then the operating system can translate those codes into characters but not directly. For example if a shift was pressed but was not released and then a code that corresponds to a press of a letter Q on a QWERTY keyboard is detected then the operating system may interpret it as an uppercase Q (unless the caps lock was on) and if it doesn't detect a release of the Q letter it may also start automatically repeating it until it detects the release of the letter Q. But the same code that is sent for the letter Q on a QWERTY keyboard is sent for the letter A on an AZERTY keyboard. It doesn't matter what is printed on the key, the physical location and electrical connection of the key is what matters.

    Now, there are some modules on npm that you may take a look at:

    but I doubt you will actually find something useful because yu normally don't work on such a low level of keyboard hardware with JavaScript or Node.

    If you don't find anything useful on npm and you end up rewriting the table that you included in JavaScript then consider publishing it on npm so others could use it.

    Update

    I just found this module:

    It seems to include some of the codes that you need. You may be able to get it from its source code.