MULTIKEY , Is the code correct?

New to FreeBASIC? Post your questions here.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: MULTIKEY , Is the code correct?

Post by speedfixer »

My early cls : print etc. is just to give a clear space to view the output from the top of page.

As I said in the remark: I would never print or do any extra activity during a critical test loop.
I run code like this in seperate threads and export (retrieve?) the value from a 'main' control loop.

But not in the console: any (Linux for sure) console is just way too slow for good, consisitent program execution if you have time constraints anywhere in your program. You don't need to use any graphics, just use a graphics window to get away from the console.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: MULTIKEY , Is the code correct?

Post by fxm »

fxm wrote: Nov 17, 2022 22:50 Misbehavior of Multikey when using text mode screen

I have already noticed (under Windows) with Multikey (with text mode screen) that a code structure like:

Code: Select all

Do
    For I As Long = 0 TO 255
        If Multikey(I) Then
            If I = n1 Then .....
            If I = n2 Then .....
            .....
            If I = 1 Then Exit Do  '' escape
        End If
    Next I
    Sleep 25, 1
Loop
always works, which is not always the case if we only test the useful values with Multikey.

Otherwise even more abruptly, by adding only the following line (which seems useless) as the first line of the scanning loop of the useful values for Multikey:
For I As Long = 0 To 255 : Multikey(I) : Next I
this unblocked the cases of not working (under Windows with text mode screen).

Looks like this swipe sequence resets the behavior.
Under Windows, the window title changes (most of time) right after launch then it works.

- Example (under Windows with text mode screen) that does not work most of the time:

Code: Select all

Const SC_Q = &h10, SC_ESCAPE = &h01, SC_SPACE = &h39
Do
    If Multikey(SC_ESCAPE) Or Multikey(SC_Q) Then Exit Do
    If Multikey(SC_SPACE) Then
        ? "Space key pressed"
        While Multikey(SC_SPACE)
            Sleep 10, 1
        Wend
        ? "Space key released"
    End If
    Sleep 50, 1
Loop

- Modified example (under Windows with text mode screen) that still works:

Code: Select all

Const SC_Q = &h10, SC_ESCAPE = &h01, SC_SPACE = &h39
Do
    For I As Long = 0 To 255
        If Multikey(I) Then
            If I = SC_ESCAPE Or I = SC_Q Then Exit Do
            If I = SC_SPACE Then
                ? "Space key pressed"
                While Multikey(SC_SPACE)
                    Sleep 10, 1
                Wend
                ? "Space key released"
            End If
        End If
    Next I
    Sleep 50, 1
Loop

- Example (under Windows with text mode screen) that works, where just a first line of code is added in the initial loop:

Code: Select all

Const SC_Q = &h10, SC_ESCAPE = &h01, SC_SPACE = &h39
Do
    For I As Long = 0 To 255 : Multikey(I) : Next I
    If Multikey(SC_ESCAPE) Or Multikey(SC_Q) Then Exit Do
    If Multikey(SC_SPACE) Then
        ? "Space key pressed"
        While Multikey(SC_SPACE)
            Sleep 10, 1
        Wend
        ? "Space key released"
    End If
    Sleep 50, 1
Loop


[edit]
In addition, for use under Linux, adding 'Cls' as the first line of code is mandatory for these 3 examples to work well.
Last edited by fxm on Nov 19, 2022 9:43, edited 1 time in total.
Reason: Updated according to the following posts from @hhr.
hhr
Posts: 205
Joined: Nov 29, 2019 10:41

Re: MULTIKEY , Is the code correct?

Post by hhr »

@fxm
Your programs do not run in Linux. I have to write 'print', 'cls' or 'locate' as the first line and they run as good as in Windows.

@speedfixer
Please try to comment out the 'cls : print...' line. In Linux your program does not run correctly without this line. Try 'shift+left' and 'shift+right'.

Does anyone have an explanation for this strange behavior?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: MULTIKEY , Is the code correct?

Post by fxm »

hhr wrote: Nov 18, 2022 18:12 @fxm
Your programs do not run in Linux. I have to write 'print', 'cls' or 'locate' as the first line and they run as good as in Windows.

Adding 'Cls' as the first line of code, which examples precisely (of the 3 from my last post) work well on Linux ?
hhr
Posts: 205
Joined: Nov 29, 2019 10:41

Re: MULTIKEY , Is the code correct?

Post by hhr »

All programs in this topic show the same behavior.

I tested with 'lubuntu-22.04-desktop-amd64' and with 'debian-live-11.5.0-amd64-lxqt' in VirtualBox.
In Lubuntu I tried with gas32, gas64, gcc32, gcc64, in Debian I tried with gas64, gcc64.

It seems adding 'Cls' as the first line of code makes all programs working well in Windows and Linux.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: MULTIKEY , Is the code correct?

Post by fxm »

Thanks.
I have complemented my original post accordingly.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: MULTIKEY , Is the code correct?

Post by coderJeff »

Here's what I found:

When MULTIKEY is called for the first time some intialization occurs in linux / win32 / dos:
- linux: start a background thread to call keyboard and mouse handlers in a a thread loop that runs until program exits
- win32: temporarily set the console title and perform a winapi FindWindow to get the console HWND - this must match GetForegroundWindow() to get any further in the logic to check key code states
- dos: hook the Keyboard interrupt service routine (ISR) and interact with the keyboard I/O directly

This lazy initialization approach was probably used so that the extra key handling code isn't initialized unless it is actually needed and we don't mess with other terminal (console) operations like redirected input.

On linux (unix), I notice the only synchronization when creating the background thread is a 'usleep(50000)' with the rtlib source comment /* Let the handler execute at least once to fill in states */.

So in theory:

Code: Select all

var tmp = multikey(0) '' first call initializes multikey handling
sleep 100,1           '' let initialization complete
But since INKEY, MULTIKEY, SLEEP all dependant on same keyboard device (real or virtualized) this seems could also be unreliable.

A better, but probably also involved feature addition would be an expanded SCREEN 0 syntax that allows specifying what kind of console behaviours are expected later in the user program - resize / don't resize / multikey / re-directed I/O / etc.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: MULTIKEY , Is the code correct?

Post by VANYA »

@coderJeff

In DOS, there is a general bug with Multikey and an extended key (eg SHIFT). There, the click flag is not reset. After pressing LSHIFT+LEFT, the SHIFT key is permanently marked as pressed and nothing can be done about it. But this problem is only in the console, in Gfxlib the flag is reset to zero. If I understand correctly, then this is the got_extended_key flag in the io_multikey.c file
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: MULTIKEY , Is the code correct?

Post by coderJeff »

@VANYA, sorry, I kind of have a love / hate relationship with the DOS port. It's where fb started and I feel like I still have plenty of knowledge about how DOS programs should work, but I find developing on a modern system problematic, and I feel like it's a fight all the time to make any progress in supporting or improving DOS version.

Is the general bug you describe a separate issue? Or is it same issue as the behaviour seen at program start? Or we don't know yet?
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: MULTIKEY , Is the code correct?

Post by VANYA »

coderJeff wrote: Nov 25, 2022 12:46 @VANYA, sorry, I kind of have a love / hate relationship with the DOS port. It's where fb started and I feel like I still have plenty of knowledge about how DOS programs should work, but I find developing on a modern system problematic, and I feel like it's a fight all the time to make any progress in supporting or improving DOS version.

Is the general bug you describe a separate issue? Or is it same issue as the behaviour seen at program start? Or we don't know yet?
Here is a simple example:

Code: Select all

cls
do
	while inkey<>"" : wend
	if multikey(&h2a) andalso multikey(&h4B) then
		? "Press SHIFT+LEFT"
		sleep(100)
	EndIf	
Loop until multikey(1) ' esc
If you press only once SHIFT + LEFT , and then you can just always press LEFT and the text will always be printed.

As for the first problem on the DOS mentioned in the topic title, I'm not sure.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: MULTIKEY , Is the code correct?

Post by coderJeff »

Are you running in an emulator? virtual machine? I don't have anything set-up at the moment that would resemble real hardware for testing DOS.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: MULTIKEY , Is the code correct?

Post by dodicat »

I have just started up an old computer Xp + win 98
I downloaded the latest fb dos, and got into dos via win 98.
I get the same results with the multikey code (simple example),
Start off with shift + left, then left on it's own will print the text.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: MULTIKEY , Is the code correct?

Post by VANYA »

coderJeff wrote: Nov 26, 2022 15:53 Are you running in an emulator? virtual machine? I don't have anything set-up at the moment that would resemble real hardware for testing DOS.
I tried on a virtual machine, but dodicat confirmed (thanks to him) that the problem exists on real hardware as well.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: MULTIKEY , Is the code correct?

Post by coderJeff »

From 2005: I notice this change:
* DOS: MULTIKEY now respects the extended scan codes and suppresses 0x2A
to get rid of the spurious SC_LSHIFT signals
VANYA wrote: Nov 24, 2022 16:51 But this problem is only in the console, in Gfxlib the flag is reset to zero. If I understand correctly, then this is the got_extended_key flag in the io_multikey.c file
And that indeed appears to be the problem that 'got_extended_key flag' is never reset and scan code '2a' is suppressed - forever.

So either:
1) Keep the scan code 2a suppression but then add a 'got_extended_key = FALSE' immediately after 2a is suppressed.
2) Remove the scan code 2a suppression and write the keyboard ISR better.

I can't think of any reason to suppress the left shift key. Maybe other scan codes that have '2a' as part of a multi byte scan sequence aren't actually being handled?
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: MULTIKEY , Is the code correct?

Post by VANYA »

coderJeff wrote: Nov 28, 2022 15:46 From 2005: I notice this change:
* DOS: MULTIKEY now respects the extended scan codes and suppresses 0x2A
to get rid of the spurious SC_LSHIFT signals
VANYA wrote: Nov 24, 2022 16:51 But this problem is only in the console, in Gfxlib the flag is reset to zero. If I understand correctly, then this is the got_extended_key flag in the io_multikey.c file
And that indeed appears to be the problem that 'got_extended_key flag' is never reset and scan code '2a' is suppressed - forever.

So either:
1) Keep the scan code 2a suppression but then add a 'got_extended_key = FALSE' immediately after 2a is suppressed.
2) Remove the scan code 2a suppression and write the keyboard ISR better.

I can't think of any reason to suppress the left shift key. Maybe other scan codes that have '2a' as part of a multi byte scan sequence aren't actually being handled?
I don't even know why SC_LSHIFT was suppressed and what false signals are being sent. As for other SHIFT+Letters combinations, there are no problems with them. You can always put freedos on a virtual system. I have 2 different emulators installed: virtualbox+freedos , 86box+freedos. Both emulators work well for almost all tasks. 86box still supports sound ,but virtualbox supports network better (easier to make data sync with main system).
Post Reply