Log In  


Cart #54654 | 2018-08-03 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
23

Hello ! It seems PICO-8 now has the ability to read the true keyboard, at least the normal keys, alphanumeric, ENTER, TAB, Backspace, and others.

So I thought I would post a working example of how to make use of this.

But does it have the ability to read raw keystrokes ? Where you can press an arrow key and the result is returned immediately - not requiring a delay as set by standard keyboard entries ?

pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
-- global+main ----------------

ins=[[
  ** real keyboard input **

    from the lab of dw817

http://writerscafe.org/dw817

  thanks to tobiasvl for
  memory tweak locations!

can you make improvements to
this code or its functions ?
please do ! it  should
benefit everyone ...
]]

asci="\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\16\17\18\19\20\21\22\23\24\25\26\27\28\29\30\31\32\33\34\35\36\37\38\39\40\41\42\43\44\45\46\47\48\49\50\51\52\53\54\55\56\57\58\59\60\61\62\63\64\65\66\67\68\69\70\71\72\73\74\75\76\77\78\79\80\81\82\83\84\85\86\87\88\89\90\91\92\93\94\95\96\97\98\99\100\101\102\103\104\105\106\107\108\109\110\111\112\113\114\115\116\117\118\119\120\121\122\123\124\125\126\127\128\129\130\131\132\133\134\135\136\137\138\139\140\141\142\143\144\145\146\147\148\149\150\151\152\153\154\155\156\157\158\159\160\161\162\163\164\165\166\167\168\169\170\171\172\173\174\175\176\177\178\179\180\181\182\183\184\185\186\187\188\189\190\191\192\193\194\195\196\197\198\199\200\201\202\203\204\205\206\207\208\209\210\211\212\213\214\215\216\217\218\219\220\221\222\223\224\225\226\227\228\229\230\231\232\233\234\235\236\237\238\239\240\241\242\243\244\245\246\247\248\249\250\251\252\253\254\255"

-- main program ---------------
function main()

cls()
print(ins,8,8,5)
spr(1,60,16)
poke(24365,1) -- mouse+key kit

t=""
print("type in some text:",28,100,11)
repeat
  grect(0,108,128,5)
  print(t,64-len(t)*2,108,6)
  grect(64+len(t)*2,108,3,5,8)
  flip()
  grect(64+len(t)*2,108,3,5,0)
  if stat(30)==true then
    c=stat(31)
    if c>=" " and c<="z" then
      t=t..c
    elseif c=="\8" then
      t=fnd(t)
    elseif c!="\13" then
      cls()
      color(7)
      print("raw key:")
      print(asc(c))
      stop()
    end
  end
until c=="\13"

end --main()

-->8
-- functions ------------------

-- grect: draw proper rectangle
function grect(h,v,x,y,c)
  rectfill(h,v,h+x-1,v+y-1,c)
end --grect(.)

-- return string minus last chr
function fnd(a)
  return sub(a,1,#a-1)
end--fnd(.)

-- len: return string length
function len(a)
  return #a
end -- len(.)

-- return pos # of str b in a
function instr(a,b)
local r=0
  if (a==null or a=="") return 0
  if (b==null or b=="") return 0
  for i=1,#a-#b+1 do
    if sub(a,i,i+#b-1)==b then
      r=i
      return r
    end
  end
  return 0
end --instr(.)

-- return ascii id of character
function asc(a)
  return instr(asci,a)
end --asc(.)

main()

__gfx__
00000000006060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000006060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700007060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000070006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000733333600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
007007006bbbbbd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000066666dd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__label__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000005050505000005550555055505000000050505550505055500550555055505500000055505500555050505550000050505050000000000000
00000000000000000500050000005050500050505000000050505000505050505050505050505050000005005050505050500500000005000500000000000000
00000000000000005550555000005500550055505000000055005500555055005050555055005050000005005050555050500500000055505550000000000000
00000000000000000500050000005050500050505000000050505000005050505050505050505050000005005050500050500500000005000500000000000000
00000000000000005050505000005050555050505550000050505550555055505500505050505550000055505050500005500500000050505050000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000606000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000606000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000706000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000007000600000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000073333360000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000006bbbbbd0000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000066666dd0000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000055505550055055500000555050505550000050005550555000000550555000005500505055505500555000000000000000000000
00000000000000000000000050005050505055500000050050505000000050005050505000005050500000005050505050500500005000000000000000000000
00000000000000000000000055005500505050500000050055505500000050005550550000005050550000005050505055500500005000000000000000000000
00000000000000000000000050005050505050500000050050505000000050005050505000005050500000005050555050500500005000000000000000000000
00000000000000000000000050005050550050500000050050505550000055505050555000005500500000005550555055505550005000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000505055505550555000000050005050505550555055505550555005500550555055505550000005505550055000505500505055505500555000000000
00000000505005000500505005000500050050505050050005005000505050005000505050005000000050505050500005005050505050500500005000000000
00000000555005000500555000000500050050505500050005005500550055505000555055005500000050505500500005005050505055500500005000000000
00000000505005000500500005000500050055505050050005005000505000505000505050005000000050505050505005005050555050500500005000000000
00000000505005000500500000005000500055505050555005005550505055000550505050005550050055005050555050005550555055505550005000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000005550505055505500505005500000555005500000555005505550555055500550505050000000555005505550000000000000000000000000
00000000000000000500505050505050505050000000050050500000050050505050050050505000505050000000500050505050000000000000000000000000
00000000000000000500555055505050550055500000050050500000050050505500050055505550505050000000550050505500000000000000000000000000
00000000000000000500505050505050505000500000050050500000050050505050050050500050555050000000500050505050000000000000000000000000
00000000000000000500505050505050505055000000050055000000050055005550555050505500050055500000500055005050000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000005550555055500550555050500000555050505550555050500000500005500550555055505550055055000550050000000000000000000000
00000000000000005550500055505050505050500000050050505000505050500000500050505000505005000500505050505000050000000000000000000000
00000000000000005050550050505050550055500000050050505500555055000000500050505000555005000500505050505550050000000000000000000000
00000000000000005050500050505050505000500000050055505000505050500000500050505000505005000500505050500050000000000000000000000000
00000000000000005050555050505500505055500000050055505550505050500000555055000550505005005550550050505500050000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000055055505500000050500550505000005550555050505550000055505550555055500550505055505550555055005550055000005550055000000000
00000000500050505050000050505050505000005550505050505000000005005550505050505050505050005550500050500500500000000500505000000000
00000000500055505050000055505050505000005050555055005500000005005050555055005050505055005050550050500500555000000500505000000000
00000000500050505050000000505050505000005050505050505000000005005050500050505050555050005050500050500500005000000500505000000000
00000000055050505050000055505500055000005050505050505550000055505050500050505500050055505050555050500500550000000500550000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000555050505550055000000550055055005550000005505550000055505550055000005550505055000550555055500550550005500000555000000000
00000000050050500500500000005000505050505000000050505050000005000500500000005000505050505000050005005050505050000000005000000000
00000000050055500500555000005000505050505500000050505500000005000500555000005500505050505000050005005050505055500000055000000000
00000000050050500500005000005000505050505000000050505050000005000500005000005000505050505000050005005050505000500000000000000000
00000000050050505550550000000550550055505550000055005050000055500500550000005000055050500550050055505500505055000000050000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000555050005550555005505550000055000550000005000000555055500000000005505050055050505000550000000000000000000000000000000000
00000000505050005000505050005000000050505050000005000000050005000000000050005050505050505000505000000000000000000000000000000000
00000000555050005500555055505500000050505050000005000000050005000000000055505550505050505000505000000000000000000000000000000000
00000000500050005000505000505000000050505050000000000000050005000000000000505050505050505000505000000000000000000000000000000000
00000000500055505550505055005550000055505500000005000000555005000000000055005050550005505550555000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000555055505500555055505550555000005550505055505550505005505500555000000000000000000000000000000000000000000000000000000000
00000000505050005050500050000500050000005000505050005050505050505050500000000000000000000000000000000000000000000000000000000000
00000000550055005050550055000500050000005500505055005500555050505050550000000000000000000000000000000000000000000000000000000000
00000000505050005050500050000500050000005000555050005050005050505050500000000000000000000000000000000000000000000000000000000000
00000000555055505050555050005550050000005550050055505050555055005050555000000500050005000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000bbb0b0b0bbb0bbb00000bbb0bb0000000bb00bb0bbb0bbb00000bbb0bbb0b0b0bbb000000000000000000000000000000000
00000000000000000000000000000b00b0b0b0b0b00000000b00b0b00000b000b0b0bbb0b00000000b00b000b0b00b000b000000000000000000000000000000
00000000000000000000000000000b00bbb0bbb0bb0000000b00b0b00000bbb0b0b0b0b0bb0000000b00bb000b000b0000000000000000000000000000000000
00000000000000000000000000000b0000b0b000b00000000b00b0b0000000b0b0b0b0b0b00000000b00b000b0b00b000b000000000000000000000000000000
00000000000000000000000000000b00bbb0b000bbb00000bbb0b0b00000bb00bb00b0b0bbb000000b00bbb0b0b00b0000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000066606660066060600000666060600000666006606060888000000000000000000000000000000000000000
00000000000000000000000000000000000000000060606060600060600000666060600000606060606060888000000000000000000000000000000000000000
00000000000000000000000000000000000000000066606660600066000000606066600000660060600600888000000000000000000000000000000000000000
00000000000000000000000000000000000000000060006060600060600000606000600000606060606060888000000000000000000000000000000000000000
00000000000000000000000000000000000000000060006060066060600000606066600000666066006060888000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
23


Heads up that it's much more efficient to do asc()/chr() as a combined lookup table:

asc,ascii={},"\0\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\16\17\18\19\20\21\22\23\24\25\26\27\28\29\30\31\32\33\34\35\36\37\38\39\40\41\42\43\44\45\46\47\48\49\50\51\52\53\54\55\56\57\58\59\60\61\62\63\64\65\66\67\68\69\70\71\72\73\74\75\76\77\78\79\80\81\82\83\84\85\86\87\88\89\90\91\92\93\94\95\96\97\98\99\100\101\102\103\104\105\106\107\108\109\110\111\112\113\114\115\116\117\118\119\120\121\122\123\124\125\126\127\128\129\130\131\132\133\134\135\136\137\138\139\140\141\142\143\144\145\146\147\148\149\150\151\152\153\154\155\156\157\158\159\160\161\162\163\164\165\166\167\168\169\170\171\172\173\174\175\176\177\178\179\180\181\182\183\184\185\186\187\188\189\190\191\192\193\194\195\196\197\198\199\200\201\202\203\204\205\206\207\208\209\210\211\212\213\214\215\216\217\218\219\220\221\222\223\224\225\226\227\228\229\230\231\232\233\234\235\236\237\238\239\240\241\242\243\244\245\246\247\248\249\250\251\252\253\254\255"
for a=0,255 do
  local c=sub(ascii,a+1,a+1)
  asc[a],asc[c]=c,a
end

> print(asc[36])
$

> print(asc["$"])
36

(Also note it's perfectly legit to put \0 in a Lua string. They're not nul-terminated.)


You know I was wondering how to get a CHR$ in there easily. And yes, I avoided "\0" thinking it would read as null.

You've got it !

Let me see if I can find all the characters.


Okay, Felice, how do you do something like this ?

for i=1,255 do
  print(i+" "+chr[i])
end

I know I need to have a STR$() routine but not sure how to go about it.


Use '..' for string concatenation:

for i=1,255 do
  print(i.." "..chr[i])
end

Note that you can convert stuff to a string in most cases just by appending it to an empty string, e.g.:

istr = ""..i

But there is also a builtin that handles some cases, like nil or tables, more gracefully, along with the option to convert numbers to their hex version:

>print(tostr(i))
15.75

>print(tostr(i,true))
0x000f.c000

>print(tostr(nil))
[nil]

>print(tostr({1,2,3}))
[table]

Oh, and there's also a tonum() that will convert numbers from decimal, hex, or binary format strings to an actual numeric value.


I'm really appreciating this, Felice ! Okay, is there a single command way to create a zero-padded number, say:

a=25
print(dec(a,4))

result is a string: 0025

in Blitz to me that is:

Function dec$(n,l)
	Local r$,i
	For i=Len(str$(n))+1 To l
		r$=r$+"0"
	Next
	r$=r$+str$(n)
	Return Right$(r$,l)
EndFunction

I would usually just do something like this:

i_as_str4 = sub("000"..i, -4)

Wow ! Now you see how more efficient your code is compared to mine ? Yet ... I can see what you're doing here, a kind of RIGHT$().

No more than 8 digits would be needed:

asc,ascii={},"\0\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\16\17\18\19\20\21\22\23\24\25\26\27\28\29\30\31\32\33\34\35\36\37\38\39\40\41\42\43\44\45\46\47\48\49\50\51\52\53\54\55\56\57\58\59\60\61\62\63\64\65\66\67\68\69\70\71\72\73\74\75\76\77\78\79\80\81\82\83\84\85\86\87\88\89\90\91\92\93\94\95\96\97\98\99\100\101\102\103\104\105\106\107\108\109\110\111\112\113\114\115\116\117\118\119\120\121\122\123\124\125\126\127\128\129\130\131\132\133\134\135\136\137\138\139\140\141\142\143\144\145\146\147\148\149\150\151\152\153\154\155\156\157\158\159\160\161\162\163\164\165\166\167\168\169\170\171\172\173\174\175\176\177\178\179\180\181\182\183\184\185\186\187\188\189\190\191\192\193\194\195\196\197\198\199\200\201\202\203\204\205\206\207\208\209\210\211\212\213\214\215\216\217\218\219\220\221\222\223\224\225\226\227\228\229\230\231\232\233\234\235\236\237\238\239\240\241\242\243\244\245\246\247\248\249\250\251\252\253\254\255"
for a=0,255 do
  local c=sub(ascii,a+1,a+1)
  asc[a],asc[c]=c,a
end

function dec(a,b)
  return sub("00000000"..tostr(a),-b)
end

cls()
x=2 y=0
for i=32,154 do
  print(dec(i,3),x,y,5)
  print(asc[i],x+12,y,7)
  x=x+18
  if (x>=126) x=2 y+=6
  flip()
end

3

finaly to avoir get pause menu involved....in update:

poke(0x5f30,1)


it pauses when i press p


I love this!


Hi guys, @BGelais, @Ninjatintin, and @Saturn91. I have rewritten the whole thing ! You can find that HERE:

https://www.lexaloffle.com/bbs/?tid=48882


@dw817 Can I have permission to use this code in a cartridge? Thanks in advance! I will give credit.



[Please log in to post a comment]