Skip to content

Instantly share code, notes, and snippets.

@okyeron
Created January 24, 2019 17:33
Show Gist options
  • Save okyeron/f959f8301329e4be8a18b477145c2f39 to your computer and use it in GitHub Desktop.
Save okyeron/f959f8301329e4be8a18b477145c2f39 to your computer and use it in GitHub Desktop.
local keyb = hid.connect(3)
local keyboard_codes = {
KEY_ESC="ESC",
KEY_1="1",
KEY_2="2",
KEY_3="3",
KEY_4="4",
KEY_5="5",
KEY_6="6",
KEY_7="7",
KEY_8="8",
KEY_9="9",
KEY_0="0",
KEY_MINUS="-",
KEY_EQUAL="=",
KEY_BACKSPACE="Backspace",
KEY_TAB="TAB",
KEY_Q="Q",
KEY_W="W",
KEY_E="E",
KEY_R="R",
KEY_T="T",
KEY_Y="Y",
KEY_U="U",
KEY_I="I",
KEY_O="O",
KEY_P="P",
KEY_LEFTBRACE="{",
KEY_RIGHTBRACE="}",
KEY_ENTER="Enter",
KEY_LEFTCTRL="Leftctrl",
KEY_A="A",
KEY_S="S",
KEY_D="D",
KEY_F="F",
KEY_G="G",
KEY_H="H",
KEY_J="J",
KEY_K="K",
KEY_L="L",
KEY_SEMICOLON=";",
KEY_APOSTROPHE="'",
KEY_GRAVE="Grave",
KEY_LEFTSHIFT="Left Shift",
KEY_BACKSLASH="\\",
KEY_Z="Z",
KEY_X="X",
KEY_C="C",
KEY_V="V",
KEY_B="B",
KEY_N="N",
KEY_M="M",
KEY_COMMA=",",
KEY_DOT=".",
KEY_SLASH="/",
KEY_RIGHTSHIFT="Right Shift",
KEY_KPASTERISK="Kpasterisk",
KEY_LEFTALT="Left Alt",
KEY_SPACE=" ",
KEY_CAPSLOCK="Capslock",
KEY_F1="F1",
KEY_F2="F2",
KEY_F3="F3",
KEY_F4="F4",
KEY_F5="F5",
KEY_F6="F6",
KEY_F7="F7",
KEY_F8="F8",
KEY_F9="F9",
KEY_F10="F10",
KEY_NUMLOCK="Numlock",
KEY_SCROLLLOCK="Scroll Lock",
KEY_KP7="KP7",
KEY_KP8="KP8",
KEY_KP9="KP9",
KEY_KPMINUS="KP Minus",
KEY_KP4="KP4",
KEY_KP5="KP5",
KEY_KP6="KP6",
KEY_KPPLUS="KP Plus",
KEY_KP1="KP1",
KEY_KP2="KP2",
KEY_KP3="KP3",
KEY_KP0="KP0",
KEY_KPDOT="KP DOT",
KEY_102ND="102ND",
KEY_F11="F11",
KEY_F12="F12",
KEY_KPENTER="KP Enter",
KEY_RIGHTCTRL="Right CTRL",
KEY_KPSLASH="KP Slash",
KEY_SYSRQ="SYSRQ",
KEY_RIGHTALT="Right Alt",
KEY_HOME="Home",
KEY_UP="Up",
KEY_PAGEUP="Pageup",
KEY_LEFT="Left",
KEY_RIGHT="Right",
KEY_END="End",
KEY_DOWN="Down",
KEY_PAGEDOWN="Page Down",
KEY_INSERT="Insert",
KEY_DELETE="Delete",
KEY_PAUSE="Pause",
KEY_LEFTMETA="Left Meta",
KEY_RIGHTMETA="Right Meta",
KEY_COMPOSE="Compose"
}
local wordarray ={}
local keyinput = ""
local keyoutput = ""
local start_y = 10
function init()
screen.aa(1)
--tab.print(keyb)
tab.print(hid.devices)
--tab.print(hid.devices[keyb.index])
print(keyb.index .." " ..hid.devices[keyb.index].name)
--tab.print(hid.devices[4].ports)
redraw()
end
function draw_firstscreen()
--screen.clear()
screen.level(15)
screen.line_width(1)
screen.font_face(0)
screen.font_size(8)
screen.move(10,10)
screen.text("NORNS> ")
screen.update()
end
function textwrap(s, w, offset, prefix)
local len = string.len(s)
--print (len)
local strstore = {}
local k = 1
if len == 0 then
screen.text(prefix)
else
while k <= len do
table.insert(strstore, string.sub(s, k, k+w-1))
k = k + w
end
strposition = start_y + offset
for v in pairs(strstore) do
screen.text(prefix .. strstore[v])
screen.move(0, strposition)
strposition = strposition + offset
end
end
end
function get_key(code, val)
if keyboard_codes[code] ~= nil and val == 1 then
return(keyboard_codes[code])
end
end
function keyb.event(typ, code, val)
--print("hid.event ", typ, code, val)
keyinput = get_key(code, val)
buildword()
end
function buildword()
if keyinput ~= "Enter" then
table.insert(wordarray,keyinput)
keyoutput = table.concat(wordarray )
redraw()
else
redraw()
keyoutput = ""
wordarray = {}
end
end
-- screen redraw function
function redraw()
-- clear screen
screen.clear()
-- set pixel brightness (0-15)
screen.level(15)
screen.line_width(1)
screen.font_face(0)
screen.font_size(8)
screen.move(0,10)
textwrap(keyoutput, 15, 10, "NORNS> ")
screen.update()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment