1. get_key Processing
- Posted by tbohon Jun 15, 2016
- 1522 views
I am trying to accept a single key from the keyboard without having to press the <cr> button, convert the key code to an upper case command character and then process the character to perform the appropriate actions. The code below results in the menu line being displayed but nothing is accepted and I have to ctl-C out of the program. I'm sure I'm doing something really stupid but for the life of me I can't figure out what it is so wanted to ask sharper minds than mine for ideas and suggestions. And yes, this is 'dirty' code which I'll clean up in a switch statement but I wanted to get it working first.
Here is the code and thanks in advance for any thoughts on what I'm doing wrong:
-- prompt the user for his command clear_screen() puts(SCREEN, "Simple TC ARES Database\n") while TRUE do puts(SCREEN, "\n(a)dd, (c)ount, (d)elete, (e)dit, (f)ind, (l)ist, (p)rint, (r)eport, (q)uit: ") while 1 do integer code = get_key() if code != -1 then command = 'Z' if code = 65 or code = 97 then command = 'A' elsif code = 67 or code = 99 then command = 'C' elsif code = 68 or code = 100 then command = 'D' elsif code = 69 or code = 101 then command = 'E' elsif code = 70 or code = 102 then command = 'F' elsif code = 76 or code = 108 then command = 'L' elsif code = 80 or code = 112 then command = 'P' elsif code = 82 or code = 114 then command = 'R' elsif code = 81 or code = 113 then command = 'Q' end if end if end while if command = 'A' then add() elsif command= 'C' then show_count(SCREEN) elsif command = 'D' then delete() elsif command = 'E' then edit_record() elsif command = 'F' then find_name() elsif command = 'Q' then exit elsif command = 'R' then report(SCREEN) elsif command = 'L' then list(SCREEN) elsif command = 'P' then printer = open("PRN", "w") if printer = -1 then puts(SCREEN, "Can't open printer device\n") else list(printer) puts(printer, FORM_FEED) close(printer) end if else puts(SCREEN, "\n\nInvalid Command: " & command & "\n") end if end while
Tom
2. Re: get_key Processing
- Posted by Mike777b Jun 15, 2016
- 1496 views
I would make two changes to the code. First, remove the command = "Z" line because it will cause the final display to always display the character Z no matter what the invalid command actually was. Second, you need to eliminate the "while 1 do" line and its corresponding "end while" statement.
mike
3. Re: get_key Processing
- Posted by _tom (admin) Jun 15, 2016
- 1459 views
change from get_key() to
include std/console.e --- wait_key() ---
4. Re: get_key Processing
- Posted by ChrisB (moderator) Jun 16, 2016
- 1442 views
Hi
Yes, the way I look at is is that wait_key sits and waits for a key, whereas get_key swings by the keyboard buffer, and gets whatever is there, and then carries on.
I have had issues where the keyboard buffer has a had a few key presses in, and then wait_key is called, and takes the first in line, so sometimes it is useful to empty the keyboard buffer before calling wait_key
integer c while get_key() => 0 end while c = wait_key()
The integer c in your above code is redundant.
Also if you are using it as a menu input, make your life easier
c = upper(wait_key())
c will always be upper case.
The function keys require special handling, and are platform dependent too. Essentially they put several characters into the keyboard buffer at once.
Check the archive fro some text mode dialogs.
Cheers
Chris
5. Re: get_key Processing
- Posted by tbohon Jun 16, 2016
- 1435 views
Mike/Tom/Chris: thank you all so much!
I played with both get_key() and wait_key() but never did come up with the correct usage.
All of your suggestions make a TON of sense and I thank you for sharing your knowledge.
Have a great day!
Tom
6. Re: get_key Processing
- Posted by Mike777b Jun 16, 2016
- 1414 views
Try this. You might need to change the include reference to a different directory to make sure the program finds console.e.
include ../../../eu405/include/std/console.e -- prompt the user for his command clear_screen() integer SCREEN = 1 puts(SCREEN, "Simple TC ARES Database\n") object command while 1 do puts(SCREEN,"\n(a)dd, (c)ount, (d)elete, (e)dit, (f)ind, (l)ist, (p)rint, (r)eport, (q)uit: ") -- while 1 do -- integer code = get_key() integer code = wait_key() if code != -1 then -- command = 'Z' command = code if code = 65 or code = 97 then command = 'A' elsif code = 67 or code = 99 then command = 'C' elsif code = 68 or code = 100 then command = 'D' elsif code = 69 or code = 101 then command = 'E' elsif code = 70 or code = 102 then command = 'F' elsif code = 76 or code = 108 then command = 'L' elsif code = 80 or code = 112 then command = 'P' elsif code = 82 or code = 114 then command = 'R' elsif code = 81 or code = 113 then command = 'Q' end if end if -- end while if command = 'A' then -- add() puts(SCREEN, "\n\nValid Command: " & command & "\n") elsif command= 'C' then -- show_count(SCREEN) puts(SCREEN, "\n\nValid Command: " & command & "\n") elsif command = 'D' then -- delete() puts(SCREEN, "\n\nValid Command: " & command & "\n") elsif command = 'E' then -- edit_record() puts(SCREEN, "\n\nValid Command: " & command & "\n") elsif command = 'F' then -- find_name() puts(SCREEN, "\n\nValid Command: " & command & "\n") elsif command = 'Q' then exit puts(SCREEN, "\n\nValid Command: " & command & "\n") elsif command = 'R' then -- report(SCREEN) puts(SCREEN, "\n\nValid Command: " & command & "\n") elsif command = 'L' then -- list(SCREEN) puts(SCREEN, "\n\nValid Command: " & command & "\n") elsif command = 'P' then -- printer = open("PRN", "w") puts(SCREEN, "\n\nValid Command: " & command & "\n") -- if printer = -1 then -- puts(SCREEN, "Can't open printer device\n") -- else -- list(printer) -- puts(printer, FORM_FEED) -- close(printer) -- end if else puts(SCREEN, "\n\nInvalid Command: " & command & "\n") end if end while
mike
7. Re: get_key Processing
- Posted by andi49 Jun 16, 2016
- 1390 views
Hi
what's about this?
include std\text.e include std\console.e atom SCREEN=1,TRUE=1,command,printer,FORM_FEED=12 procedure delete() puts(1,"\ndelete is not a good name. It's an builtin name\n") end procedure procedure add() puts(1,"\nadd\n") end procedure procedure show_count(atom x) puts(1,"\nshow_count\n") end procedure procedure edit_record() puts(1,"\nshow_record\n") end procedure procedure find_name() puts(1,"\nfind_name\n") end procedure procedure report(atom x) puts(1,"\nreport\n") end procedure procedure list(atom x) puts(1,"\nlist\n") end procedure -- prompt the user for his command clear_screen() puts(SCREEN, "Simple TC ARES Database\n") while TRUE do puts(SCREEN, "\n(a)dd, (c)ount, (d)elete, (e)dit, (f)ind, (l)ist, (p)rint, (r)eport, (q)uit: ") integer code = upper(wait_key()) if find(code,{'A','C','D','E','F','L','P','P','R','Q'}) then command=code else command='Z' end if if command = 'A' then add() elsif command= 'C' then show_count(SCREEN) elsif command = 'D' then delete() elsif command = 'E' then edit_record() elsif command = 'F' then find_name() elsif command = 'Q' then exit elsif command = 'R' then report(SCREEN) elsif command = 'L' then list(SCREEN) elsif command = 'P' then printer = open("PRN", "w") if printer = -1 then puts(SCREEN, "\nCan't open printer device\n") else list(printer) puts(printer, FORM_FEED) close(printer) end if else puts(SCREEN, "\n\nInvalid Command: " & command & "\n") end if end while
Andreas
8. Re: get_key Processing
- Posted by jmduro Jun 18, 2016
- 1344 views
Here is what I use:
include std/console.e public constant ENTER={13}, ESC={27}, SPACE={32}, BACKSPACE = {8}, TAB = {9}, UP = {1016400}, DOWN = {1016432}, LEFT = {1016384}, RIGHT = {1016416}, PAGE_UP = {1016320}, PAGE_DOWN = {1016336}, HOME = {1016368}, CTRL_LEFT = {1024176}, START_LEFT = {1020096}, ALT = {1032400}, ALT_GR = {1020096}, START_RIGHT = {1016480}, CTXT_MENU = {1016496}, CTRL_RIGHT = {1020096}, END = {1016352}, INSERT = {1016288}, DELETE = {1016304} public constant F_ = { {1017600}, {1017616}, {1017632}, {1017648}, {1017664}, {1017680}, {1017696}, {1017712}, {1017728}, {1017744}, {1017760}, {1017776} } ------------------------------------------------------------------------------- public function getKeys() sequence result integer code result = {} code = wait_key() while code != -1 do result = append(result,code) code = get_key() end while -- printf(f_debug, "getKeys = %s\n", {sprint(result)}) return result end function
Example of usage:
atom fl_out = 0 while fl_out = 0 do ex = getKeys() if equal(ex, HOME) then -- add your code elsif equal(ex, END) then -- add your code elsif equal(ex, LEFT) then -- add your code elsif equal(ex, RIGHT) then -- add your code elsif equal(ex, DELETE) then -- add your code elsif equal(ex, INSERT) then -- add your code elsif find(ex, F_ & {UP, DOWN, ENTER, TAB, ESC}) then fl_out = 1 elsif equal(ex, BACKSPACE) then -- add your code elsif (ex[1] > 31) and (ex[1] < 255) then -- add your code end if end while
Jean-Marc