Multikey issue with inkey

General FreeBASIC programming questions.
Post Reply
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Multikey issue with inkey

Post by exagonx »

Hi guys.
Today I wanted to make a small program to facilitate some tasks and I wrote this code:

Code: Select all

EndLoop = 0
do 

Key = ""
Key = inkey
KeyCommand = 0



if multikey(&h1D) then 
	if multikey(&h52) then KeyCommand = 3 
	if multikey(&h53) then KeyCommand = 4
	if multikey(&h1F) then KeyCommand = 2
	Key = ""
end if

if Key = chr(27) then EndLoop = 1: Key = ""

if len(Key) > 0 and asc(Key) <> 255 then  locate 10,1: MyString = MyString & Key:  print MyString
if KeyCommand = 1 then locate 10,10: print space(len(MyString)): MyString = ""
if KeyCommand = 2 then locate 5,11: print space(20):locate 5,11: print "CTRL + S"
if KeyCommand = 3 then locate 5,11: print space(20):locate 5,11: print "CTRL + INS"
if KeyCommand = 4 then locate 5,11: print space(20):locate 5,11: print "CTRL + CANC"
sleep 1

loop until EndLoop > 0

But pressing the CTRL + INS or S or DEL keys Sometimes the message appears and sometimes nothing happens.
I need this code to create a management of the CTRL + KEY and ALT + KEY keys where they are not manageable with CHR ()
Have you ever had this kind of problem?

In any case, thank you for your attention
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Multikey issue with inkey

Post by paul doe »

Moved to 'General' as this is more a general inquiry than a 'Tip and Trick'.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Multikey issue with inkey

Post by speedfixer »

First: longer main loop sleep with no interaction - say ... SLEEP 30, 1

The OS needs time to collect the keystroke and return it to you. It can happen so fast that you looped after your keystroke, then picked up 20 more keys and several non-keypresses - yet you never saw them. It also takes a LONG time to print to screen. The loop will have moved on before the screen write is finished.

Next, when something/anything IS detected - put an extra SLEEP 500, 1 just for that case. Then you know something happened. You can change/delete that later.

Then - maybe most important, clear after ANY key is pressed or it will stay in the buffer until you loop and pick it up again - or see multiple apparent keystrokes with MULTIKEY.

After a detected key (or keys) I simply:

Code: Select all

while inkey <> " : sleep 5,1 : wend
That loop will clear all keypresses from the buffer for the next program DO loop.

This will let you see the effects of different SLEEP timings and the real world key processing speeds.
For fun, put a loop counter in the loop to see how fast it is going. Fingers as an input don't really work that fast.

Correctly handling key presses can be simple - until you expect to use all the speed and power of modern processors. Then it can become very non-trivial quickly. Adding threads adds an entirely different set of considerations.

david
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Multikey issue with inkey

Post by speedfixer »

Also, are you in a console/terminal or in a graphics screen?

I use Linux, only. Don't know about Windows.

For Linux, the FB graphics system is is WAY faster than a console or terminal. From 50 to 500 times, depending on code and systems.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Multikey issue with inkey

Post by exagonx »

speedfixer wrote:Also, are you in a console/terminal or in a graphics screen?
.
Its on lInux command line Anyway appair like inkey and multikey doesnt work in the same loop correctly

if I remove Inkey then multikey work

Thank you for your answer.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Multikey issue with inkey

Post by speedfixer »

They CAN work together.

An inkey will remove the key action from the buffer, so a multikey after inkey will miss it.
(If the loop is very fast then some single keys will be missed, but you will have many multiple multikey responses.)

If the inkey is after multikey, you have 2 opportunities to catch a key press.

Multikey reads the key status *ONLY* at the time the multikey test code is processed. Near real time.
The loop has to be fast enough to pass through that multikey test WHEN the button is pressed.
A typical keypress is between 10+ milliseconds and 50+ milliseconds, unless slow fingers or holding the key.

If only a single, simple loop is used, the problems become obvious very quickly. (Part of my question whether in console or FB graphics. I suspect Windows console is faster than Linux console.)

Inkey reads a buffer holding key presses, popping one off the buffer for each inkey use, whether you use it (assign to a var) or not.

If you need very fast response to some particular key set and reduced latency, multikey is a simple answer.
You still need to clear the buffer sooner or later. When you leave that loop and want keys later, the buffer will THEN spill old keys.

If the keypress is critical - my answer:

Use a separate thread ONLY for keyboard input, with no other key tests anywhere else in the code. (edit: except perhaps to kill the thread and program from the main loop. A design choice.) Expose any data *as needed* for the specific app - either a serial buffer, instantaneous value, or boolean key array.

This is what happens in the OS and FB anyway. This just gives you more precise control once you understand how it works.
Unless there is text editing functions needed, most apps do not need 'noise' from unwanted keys.

If threading is still on the learning list, then you must keep track of how fast any of your main or side loops take and deal with any leftover keys when you move to a different section of your code. Clear the buffer when you move from function to function, for example.

Learning takes effort. For me, learning is the fun part. A working app is just extra sauce.

david
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Multikey issue with inkey

Post by exagonx »

speedfixer wrote:They CAN work together.

david
Its work together but for what I need I cant use it in same time because I cant slow with sleep (because the keyboard is not a normal keyboard but optical sensor )
And I cant use thread because become useless the rest of software.

Apologies if I give only what I cant do, but I have no idea of how do it

I made a code where I use only multikeys without inkey but each key become double like " hheelloo "

multikey is a good way for make a real time game with my knowledge I have no idea how made the code work correctly

Thank you for suggestion and apologize if Im not able to use it
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Multikey issue with inkey

Post by fxm »

If you use a command window (and not a graphics window), you must disable 'Ctrl key shortcuts' in the properties of the window.
Otherwise, use a graphics window.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Multikey issue with inkey

Post by exagonx »

fxm wrote:If you use a command window (and not a graphics window), you must disable Ctrl key shortcuts in the properties of the window.
Otherwise, use a graphics window.
Thank you this solve the problem where I cant use CTRL+INS and Shift +INS with inkey Now appair that I dont need anymore multikey I can do it with inkey .
Post Reply