How to grab escape-sequence keycodes?

General FreeBASIC programming questions.
Post Reply
skystrick
Posts: 104
Joined: May 19, 2013 23:25
Location: Florida

How to grab escape-sequence keycodes?

Post by skystrick »

Hi, I am running a console-mode program on an RS232 text dumb terminal.

The terminal hardware sends escape sequence keycodes for the F-keys, arrow keys, etc.

Example: Pressing F1 makes the terminal send: Ctrl+A, @, <CR>

In other words Chr$(1), Chr$(64), Chr$(13)

Problem: Inkey, Getkey, and Multikey return unpredictable results with these long multi-character scancodes.

Problem: Keycodes are variable length. Some send 3 characters like above example, others are 1-6 characters long.

Problem: Not all keycodes end with Chr$(13), so Input # with the Cons OPENed as an input file doesn't work in all cases. Input # doen't terminate until it finds an EOL character.

Info: The RS232 terminal can be set to send hex scancodes for all keys (including QWERTY) instead of ASCII characters. However I don't know how to receive this input into my FreeBASIC program? Linux can't handle the hex scancodes internally, and because Multikey and Getkey map to Linux's terminfo function, they break too.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: How to grab escape-sequence keycodes?

Post by grindstone »

Maybe try Get# instead of Input# (sorry, can't test it here now due to lack of time :-( )
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: How to grab escape-sequence keycodes?

Post by D.J.Peters »

"... Inkey, Getkey, and Multikey ..."

What have this functions to do with a RS-232 connection ?

In FreeBASIC a RS-232 connection is like a file read / write operation.

KeyPgOpenCom

Joshy
skystrick
Posts: 104
Joined: May 19, 2013 23:25
Location: Florida

Re: How to grab escape-sequence keycodes?

Post by skystrick »

D.J.Peters wrote:"... Inkey, Getkey, and Multikey ..."

What have this functions to do with a RS-232 connection ?

In FreeBASIC a RS-232 connection is like a file read / write operation.

KeyPgOpenCom

Joshy
The RS232 terminal is a text console, i.e. attached to Linux as stdin/stdout.

It is accessed inside FreeBASIC using "Open Cons:"
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: How to grab escape-sequence keycodes?

Post by D.J.Peters »

Code: Select all

var hFile = FreeFile()

Open Cons For Input As #hFile

var aborted=false
while aborted = false
  var ASingleChar = Input(1,hFile)
  if ASingleChar<>"" then
    if ASingleChar[0] = 1 then
      var TwoChars = Input(2,hFile)    
      if TwoChars[0]=64 andalso TwoChars[1]=13 then
        print "F1 are pressed !"
      end if  
    elseif ASingleChar[0] = 12 then
      '....    
    end if  
  end if
  'if inkey()=asc(27) then aborted=true
wend
close #hFile
print "done ..."
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to grab escape-sequence keycodes?

Post by MrSwiss »

It seems, that we are talking about two different things.
This needs some clarification first.

If you are connecting to a hardware (physical) terminal, then I think:
Joshi is correct (and the FB command is: Open COM)
And, atachement to Linux Terminals IO streams, whould not be correct.
skystrick
Posts: 104
Joined: May 19, 2013 23:25
Location: Florida

Re: How to grab escape-sequence keycodes?

Post by skystrick »

MrSwiss wrote:It seems, that we are talking about two different things.
This needs some clarification first.

If you are connecting to a hardware (physical) terminal, then I think:
Joshi is correct (and the FB command is: Open COM)
And, atachement to Linux Terminals IO streams, whould not be correct.
The text terminal is on an RS232-to-USB converter, running on the Linux box as /dev/ttyUSB2

The shell session running on the terminal sees the terminal as stdin and stdout.

So using a PRINT command in fb causes output to the terminal.

The QWERTY keys work fine with Input, Getkey, Multikey, etc., because they use standard ASCII scancodes.

But the arrow, function, etc., keys do not work because they send special control codes and escape sequences unique to my terminal.

How can I capture these special control codes in my program? For example, how do I capture the "Chr$(1) & Chr$(64) & Chr$(13)" sent by the terminal when I press F1?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: How to grab escape-sequence keycodes?

Post by D.J.Peters »

If you read from stdin you use

Open Cons For Input As #hFile

then my posted solution will work

if not why

you can post the code also

Joshy
skystrick
Posts: 104
Joined: May 19, 2013 23:25
Location: Florida

Re: How to grab escape-sequence keycodes?

Post by skystrick »

D.J.Peters wrote:If you read from stdin you use

Open Cons For Input As #hFile

then my posted solution will work

if not why

you can post the code also

Joshy
Thank you D.J.Peters, your code solved my problem!
Post Reply