

(define (scancode->keycode s)
  (logor (sdl-scancode s) (bitwise-arithmetic-shift-left 1 30)))

(define-flags sdl-keycode
    (unknown   0)
    (return    (char->integer #\return))
    (escape    (char->integer #\esc))
    (backspace    (char->integer #\backspace))
    (tab    (char->integer #\tab))
    (space    (char->integer #\space))
    (exclaim (char->integer #\!))
    (quotedbl    (char->integer #\"))
    (hash    (char->integer #\#))
    (percent    (char->integer #\%))
    (dollar    (char->integer #\$))
    (ampersand    (char->integer #\&))
    (quote    (char->integer #\'))
    (leftparen    (char->integer #\())
    (rightparen    (char->integer #\)))
    (asterisk    (char->integer #\*))
    (plus    (char->integer #\+))
    (comma    (char->integer #\,))
    (minus    (char->integer #\-))
    (period    (char->integer #\.))
    (slash    (char->integer #\/))
    (0    (char->integer #\0))
    (1    (char->integer #\1))
    (2    (char->integer #\2))
    (3    (char->integer #\3))
    (4    (char->integer #\4))
    (5    (char->integer #\5))
    (6    (char->integer #\6))
    (7    (char->integer #\7))
    (8    (char->integer #\8))
    (9    (char->integer #\9))
    (colon    (char->integer #\:))
    (semicolon    (char->integer #\;))
    (less    (char->integer #\<))
    (equals    (char->integer #\=))
    (greater    (char->integer #\>))
    (question    (char->integer #\?))
    (at    (char->integer #\@))
    ;; /*
    ;;    skip uppercase letters
    ;;  */
    (leftbracket    (char->integer #\[))
    (backslash    (char->integer #\\))
    (rightbracket    (char->integer #\]))
    (caret    (char->integer #\^))
    (underscore    (char->integer #\_))
    (backquote    (char->integer #\`))
    (a    (char->integer #\a))
    (b    (char->integer #\b))
    (c    (char->integer #\c))
    (d    (char->integer #\d))
    (e    (char->integer #\e))
    (f    (char->integer #\f))
    (g    (char->integer #\g))
    (h    (char->integer #\h))
    (i    (char->integer #\i))
    (j    (char->integer #\j))
    (k    (char->integer #\k))
    (l    (char->integer #\l))
    (m    (char->integer #\m))
    (n    (char->integer #\n))
    (o    (char->integer #\o))
    (p    (char->integer #\p))
    (q    (char->integer #\q))
    (r    (char->integer #\r))
    (s    (char->integer #\s))
    (t    (char->integer #\t))
    (u    (char->integer #\u))
    (v    (char->integer #\v))
    (w    (char->integer #\w))
    (x    (char->integer #\x))
    (y    (char->integer #\y))
    (z    (char->integer #\z))

    (capslock    (sdl-scancode 'capslock))

    (f1    (scancode->keycode 'f1))
    (f2    (scancode->keycode 'f2))
    (f3    (scancode->keycode 'f3))
    (f4    (scancode->keycode 'f4))
    (f5    (scancode->keycode 'f5))
    (f6    (scancode->keycode 'f6))
    (f7    (scancode->keycode 'f7))
    (f8    (scancode->keycode 'f8))
    (f9    (scancode->keycode 'f9))
    (f10    (scancode->keycode 'f10))
    (f11    (scancode->keycode 'f11))
    (f12    (scancode->keycode 'f12))

    (printscreen    (scancode->keycode 'printscreen))
    (scrolllock    (scancode->keycode 'scrolllock))
    (pause    (scancode->keycode 'pause))
    (insert    (scancode->keycode 'insert))
    (home    (scancode->keycode 'home))
    (pageup    (scancode->keycode 'pageup))
    (delete    (char->integer #\delete))
    (end    (scancode->keycode 'end))
    (pagedown    (scancode->keycode 'pagedown))
    (right    (scancode->keycode 'right))
    (left    (scancode->keycode 'left))
    (down    (scancode->keycode 'down))
    (up    (scancode->keycode 'up))

    (numlockclear    (scancode->keycode 'numlockclear))
    (kp-divide    (scancode->keycode 'kp-divide))
    (kp-multiply    (scancode->keycode 'kp-multiply))
    (kp-minus    (scancode->keycode 'kp-minus))
    (kp-plus    (scancode->keycode 'kp-plus))
    (kp-enter    (scancode->keycode 'kp-enter))
    (kp-1    (scancode->keycode 'kp-1))
    (kp-2    (scancode->keycode 'kp-2))
    (kp-3    (scancode->keycode 'kp-3))
    (kp-4    (scancode->keycode 'kp-4))
    (kp-5    (scancode->keycode 'kp-5))
    (kp-6    (scancode->keycode 'kp-6))
    (kp-7    (scancode->keycode 'kp-7))
    (kp-8    (scancode->keycode 'kp-8))
    (kp-9    (scancode->keycode 'kp-9))
    (kp-0    (scancode->keycode 'kp-0))
    (kp-period    (scancode->keycode 'kp-period))

    (application    (scancode->keycode 'application))
    (power    (scancode->keycode 'power))
    (kp-equals    (scancode->keycode 'kp-equals))
    (f13    (scancode->keycode 'f13))
    (f14    (scancode->keycode 'f14))
    (f15    (scancode->keycode 'f15))
    (f16    (scancode->keycode 'f16))
    (f17    (scancode->keycode 'f17))
    (f18    (scancode->keycode 'f18))
    (f19    (scancode->keycode 'f19))
    (f20    (scancode->keycode 'f20))
    (f21    (scancode->keycode 'f21))
    (f22    (scancode->keycode 'f22))
    (f23    (scancode->keycode 'f23))
    (f24    (scancode->keycode 'f24))
    (execute    (scancode->keycode 'execute))
    (help    (scancode->keycode 'help))
    (menu    (scancode->keycode 'menu))
    (select    (scancode->keycode 'select))
    (stop    (scancode->keycode 'stop))
    (again    (scancode->keycode 'again))
    (undo    (scancode->keycode 'undo))
    (cut    (scancode->keycode 'cut))
    (copy    (scancode->keycode 'copy))
    (paste    (scancode->keycode 'paste))
    (find    (scancode->keycode 'find))
    (mute    (scancode->keycode 'mute))
    (volumeup    (scancode->keycode 'volumeup))
    (volumedown    (scancode->keycode 'volumedown))
    (kp-comma    (scancode->keycode 'kp-comma))
    (kp-equalsas400     (scancode->keycode 'kp-equalsas400))

    (alterase    (scancode->keycode 'alterase))
    (sysreq    (scancode->keycode 'sysreq))
    (cancel    (scancode->keycode 'cancel))
    (clear    (scancode->keycode 'clear))
    (prior    (scancode->keycode 'prior))
    (return2    (scancode->keycode 'return2))
    (separator    (scancode->keycode 'separator))
    (out    (scancode->keycode 'out))
    (oper    (scancode->keycode 'oper))
    (clearagain    (scancode->keycode 'clearagain))
    (crsel    (scancode->keycode 'crsel))
    (exsel    (scancode->keycode 'exsel))

    (kp-00    (scancode->keycode 'kp-00))
    (kp-000    (scancode->keycode 'kp-000))
    (thousandsseparator (scancode->keycode 'thousandsseparator))
    (decimalseparator  (scancode->keycode 'decimalseparator))
    (currencyunit    (scancode->keycode 'currencyunit))
    (currencysubunit  (scancode->keycode 'currencysubunit))
    (kp-leftparen    (scancode->keycode 'kp-leftparen))
    (kp-rightparen    (scancode->keycode 'kp-rightparen))
    (kp-leftbrace    (scancode->keycode 'kp-leftbrace))
    (kp-rightbrace    (scancode->keycode 'kp-rightbrace))
    (kp-tab    (scancode->keycode 'kp-tab))
    (kp-backspace    (scancode->keycode 'kp-backspace))
    (kp-a    (scancode->keycode 'kp-a))
    (kp-b    (scancode->keycode 'kp-b))
    (kp-c    (scancode->keycode 'kp-c))
    (kp-d    (scancode->keycode 'kp-d))
    (kp-e    (scancode->keycode 'kp-e))
    (kp-f    (scancode->keycode 'kp-f))
    (kp-xor    (scancode->keycode 'kp-xor))
    (kp-power    (scancode->keycode 'kp-power))
    (kp-percent    (scancode->keycode 'kp-percent))
    (kp-less    (scancode->keycode 'kp-less))
    (kp-greater    (scancode->keycode 'kp-greater))
    (kp-ampersand    (scancode->keycode 'kp-ampersand))
    (kp-dblampersand  (scancode->keycode 'kp-dblampersand))
    (kp-verticalbar (scancode->keycode 'kp-verticalbar))
    (kp-dblverticalbar (scancode->keycode 'kp-dblverticalbar))
    (kp-colon    (scancode->keycode 'kp-colon))
    (kp-hash    (scancode->keycode 'kp-hash))
    (kp-space    (scancode->keycode 'kp-space))
    (kp-at    (scancode->keycode 'kp-at))
    (kp-exclam    (scancode->keycode 'kp-exclam))
    (kp-memstore    (scancode->keycode 'kp-memstore))
    (kp-memrecall    (scancode->keycode 'kp-memrecall))
    (kp-memclear    (scancode->keycode 'kp-memclear))
    (kp-memadd    (scancode->keycode 'kp-memadd))
    (kp-memsubtract  (scancode->keycode 'kp-memsubtract))
    (kp-memmultiply   (scancode->keycode 'kp-memmultiply))
    (kp-memdivide    (scancode->keycode 'kp-memdivide))
    (kp-plusminus    (scancode->keycode 'kp-plusminus))
    (kp-clear    (scancode->keycode 'kp-clear))
    (kp-clearentry    (scancode->keycode 'kp-clearentry))
    (kp-binary    (scancode->keycode 'kp-binary))
    (kp-octal    (scancode->keycode 'kp-octal))
    (kp-decimal    (scancode->keycode 'kp-decimal))
    (kp-hexadecimal (scancode->keycode 'kp-hexadecimal))

    (lctrl    (scancode->keycode 'lctrl))
    (lshift    (scancode->keycode 'lshift))
    (lalt    (scancode->keycode 'lalt))
    (lgui    (scancode->keycode 'lgui))
    (rctrl    (scancode->keycode 'rctrl))
    (rshift    (scancode->keycode 'rshift))
    (ralt    (scancode->keycode 'ralt))
    (rgui    (scancode->keycode 'rgui))

    (mode    (scancode->keycode 'mode))

    (audionext    (scancode->keycode 'audionext))
    (audioprev    (scancode->keycode 'audioprev))
    (audiostop    (scancode->keycode 'audiostop))
    (audioplay    (scancode->keycode 'audioplay))
    (audiomute    (scancode->keycode 'audiomute))
    (mediaselect    (scancode->keycode 'mediaselect))
    (www    (scancode->keycode 'www))
    (mail    (scancode->keycode 'mail))
    (calculator    (scancode->keycode 'calculator))
    (computer    (scancode->keycode 'computer))
    (ac-search    (scancode->keycode 'ac-search))
    (ac-home    (scancode->keycode 'ac-home))
    (ac-back    (scancode->keycode 'ac-back))
    (ac-forward    (scancode->keycode 'ac-forward))
    (ac-stop    (scancode->keycode 'ac-stop))
    (ac-refresh    (scancode->keycode 'ac-refresh))
    (ac-bookmarks    (scancode->keycode 'ac-bookmarks))

    (brightnessdown (scancode->keycode 'brightnessdown))
    (brightnessup    (scancode->keycode 'brightnessup))
    (displayswitch    (scancode->keycode 'displayswitch))
    (kbdillumtoggle (scancode->keycode 'kbdillumtoggle))
    (kbdillumdown    (scancode->keycode 'kbdillumdown))
    (kbdillumup    (scancode->keycode 'kbdillumup))
    (eject    (scancode->keycode 'eject))
    (sleep    (scancode->keycode 'sleep))
    );; flags sdl-keycode   

(define-flags sdl-keymod
    (none    #x0000)
    (lshift    #x0001)
    (rshift    #x0002)
    (lctrl    #x0040)
    (rctrl    #x0080)
    (lalt    #x0100)
    (ralt    #x0200)
    (lgui    #x0400)
    (rgui    #x0800)
    (num    #x1000)
    (caps    #x2000)
    (mode    #x4000)
    (reserved    #x8000)
    (ctrl     #x00C0)
    (shift    3)
    (alt    #x0300)
    (gui    #x0c00))

