key-pressed or (read function with a time out

For the Compleat Fan
Locked
CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

key-pressed or (read function with a time out

Post by CaveGuy »

WANTED: The able to test for a key-pressed or be able to time out from a (read- like function.

The object is to be able to interrupt a loop with a key stroke. currently all input functions are blocking and wait forever for input. Sometimes when no input is forth coming we need to be able to move on.

(net-peek and (net-connect with it's timeout are good examples of non-blocking input.

In windows we have the following workaround but nothing I can figure out for the other os's

Code: Select all

(if (not _kbhit) (import "msvcrt.DLL" "_kbhit" ))
(if (not _getch) (import "msvcrt.DLL" "_getch" ))

(define (key-pressed)
  (if (= 0 (_kbhit)) nil
      (begin
         (setq keyval (_getch))
         (if (or (= keyval 0)(= keyval 224)) (CONSOLE:_getch)) 
         (setq keypress (char keyval)))))
Bob the Caveguy aka Lord High Fixer.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: key-pressed or (read function with a time out

Post by Lutz »

In version 10.7.3 a 'true' flag parameter in 'read-key' enables non-blocking behavior.

When using '(read-key true)' a 0 (zero) is returned when no key was pressed or the key code is returned when a key was pressed.

http://newlisp.nfshost.com/downloads/de ... nprogress/

Tested on macOS, Ubuntu Linux, FreeBSD, OpenBSD and MS Windows.

CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

Re: key-pressed or (read function with a time out

Post by CaveGuy »

Thank you! my only question is if 10.7.3 will be released as binaries for windows in the near future ?
Now I am retired the cost of maintaining the mickeysoft development tools has become prohibitive.
Bottom line is I no longer have a windows build environment on my current system and plan on using newlisp exclusively going forward.
Bob the Caveguy aka Lord High Fixer.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: key-pressed or (read function with a time out

Post by Lutz »

You can find an executable Windows version here:

http://newlisp.nfshost.com/downloads/de ... nprogress/

CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

Re: key-pressed or (read function with a time out

Post by CaveGuy »

thanks, but I need a 32bit one. I will go into more detail why in a different thread.
Bob the Caveguy aka Lord High Fixer.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: key-pressed or (read function with a time out

Post by Lutz »


Locked