Multikey problem - bug ? (FIXED)

General FreeBASIC programming questions.
Post Reply
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Multikey problem - bug ? (FIXED)

Post by petan »

Hi guys,
I found this problem with Multikey command.Although keybuffer is cleared with Inkey (as written in manual), in second test for Escape it has again value.
In proggy I have this line for shortjump out of writing more text or drawing more lines in For-Next cycles, but overjumped everything, even keybuffer cleared.
Written code repeats the same problem.So I am not sured, if it is a bug or not; where Multikey takes value from ?and howto totally clear that one ?

Code: Select all

#include "fbgfx.bi"
ScreenRes 1024,752,32,8,FB.GFX_no_frame		
Width 128,47

Dim Shared As String fv	

Print "Press Esc key, or qQ to end"
Do
 Sleep 1,1
    fv= InKey
If fv<>"" Then
	Cls:? "qQ to Quit"
	Locate 8,10:? "fv= ";fv
	
 'If MULTIKEY(&h1) Then
 If MULTIKEY(fb.SC_ESCAPE) Then 	
 	Locate 5,10:? "Esc 1 pressed...",
	? " fv=";fv ,	 	
 	 	While Inkey<>""
 	 		? " clearing1"; 	 		
 	 	Wend
 	 ? "empty",
 	 fv= Inkey:? " fv=";fv
 End If
While Inkey<>""
	? " clearing2";
Wend
?
 'If MULTIKEY(&h1) Then
 If MULTIKEY(fb.SC_ESCAPE) Then
 	
 	'  WHERE it takes the value from ??, inkey was cleared !
 	
 	Locate 6,10:? "Esc 2 pressed...",
	? " fv=";fv,
 	 	While Inkey<>""
 	 		? " clearing3";
 	 	Wend
 	 ? "empty",
 	 fv= Inkey:? " fv=";fv
 End If

While Inkey<>""
	? " clearing4";
Wend
?
End If
Loop Until fv="q"		

 End
I assume that "Esc 2 pressed..." wouldn't be written in any way.

Pete
Last edited by petan on Jun 08, 2012 12:20, edited 2 times in total.
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Multikey problem - bug ?

Post by fxm »

MultiKey detects if keys are pressed, independently of the keyboard input buffer.
When the program executes the instruction 'Multikey()', the key is yet pressed.
Put a small delay 'Sleep 200' after 'If fv<>"" Then', and you will see the difference (obviously using short press).
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Multikey problem - bug ?

Post by petan »

Thx, fxm.
Just tried on Linux this wildy creation (waiting for forgotting of leaved key in hard loop), here works, we 'll see what about on windoze.

Code: Select all

 'trouble code - value of pressed key is somewhere remembered
'If MULTIKEY(&h1) Then goto labelXYZ
 If MULTIKEY(fb.SC_ESCAPE) Then  goto labelXYZ

Code: Select all

 'solution by FB manual - insufficient - not works
'If MULTIKEY(&h1) Then 
 If MULTIKEY(fb.SC_ESCAPE) Then 
    While Inkey<>"":Wend
    goto labelXYZ
end if

Code: Select all

 'hard wait for forgotting of leaved key - it works
'If MULTIKEY(&h1) Then 
 If MULTIKEY(fb.SC_ESCAPE) Then 
         'new - stop here and wait until forgotten anyway
    While MULTIKEY(fb.SC_ESCAPE)
     fv= Inkey
    Wend 
    goto labelXYZ
end if
Pete
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Multikey problem - bug ? Solved temporarily ?

Post by fxm »

Another solution instead of my previous 'Sleep 200':
Wait the event 'KEY_RELEASE'

Code: Select all

#include "fbgfx.bi"
ScreenRes 1024,752,32,8,FB.GFX_no_frame      
Width 128,47

Dim Shared As String fv   
Dim e As fb.EVENT

Print "Press Esc key, or qQ to end"
Do
Sleep 1,1
    fv= InKey
If fv<>"" Then
   Cls:? "qQ to Quit"
   Locate 8,10:? "fv= ";fv

Do
   If (ScreenEvent(@e)) Then
      If e.type = fb.EVENT_KEY_RELEASE Then
         Exit Do
      End If
   End If
Loop

'If MULTIKEY(&h1) Then
If MULTIKEY(fb.SC_ESCAPE) Then    
   Locate 5,10:? "Esc 1 pressed...",
   ? " fv=";fv ,       
       While Inkey<>""
          ? " clearing1";           
       Wend
    ? "empty",
    fv= Inkey:? " fv=";fv
End If
While Inkey<>""
   ? " clearing2";
Wend
?
'If MULTIKEY(&h1) Then
If MULTIKEY(fb.SC_ESCAPE) Then
   
   '  WHERE it takes the value from ??, inkey was cleared !
   
   Locate 6,10:? "Esc 2 pressed...",
   ? " fv=";fv,
       While Inkey<>""
          ? " clearing3";
       Wend
    ? "empty",
    fv= Inkey:? " fv=";fv
End If

While Inkey<>""
   ? " clearing4";
Wend
?
End If
Loop Until fv="q"      

End
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Re: Multikey problem - bug ? Solved temporarily ?

Post by pestery »

As fxm said, MultiKey checks if the key is pressed at the time when Multikey is called. This is good for things like scrolling, moving, steering, and so on, where you will be holding down the key for some amount of time. For quick key presses, such as jumping, typing text, and so forth, you are better off using either InKey or ScreenEvent. Each have their own uses. A combination of Multikey and ScreenEvent works well.
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Multikey problem - bug ? Solved temporarily ?

Post by petan »

Thx, guys! It's OK now, on windoze too.Both Lin Win works as well.

Before - flowing code

Image

Now - with fix
Image

Code is just tuned a bit

Code: Select all

#include "fbgfx.bi"
ScreenRes 1024,752,32,8,FB.GFX_no_frame	
Width 128,47

Declare Sub bfMKey(ByVal keyID As Integer)
Dim Shared As String fv	
Dim As Integer keyID

Print "Press Esc key, or qQ to end"
Do
   Sleep 1,1
   fv= InKey
If fv<>"" Then
	Cls
	Locate 8,10:? " fv=";fv	 	

 'If MULTIKEY(&h1) Then
 'If MULTIKEY(fb.SC_ESCAPE) Then 	
keyID=fb.SC_ESCAPE
If MULTIKEY(keyID) Then
 	Locate 5,10:? "Esc 1 pressed...",
	? " fv=";fv 	',	 	
 	 bfMKey(keyID)	'be freeed multikey / hard wait until recognised freed key by Multikey itself /
 	 'goto labelXYZ
 End If

 'If MULTIKEY(&h1) Then
 'If MULTIKEY(fb.SC_ESCAPE) Then
 keyID=fb.SC_ESCAPE
 If MULTIKEY(keyID) Then 	
 	Locate 6,10:? "Esc 2 pressed...",
	? " fv=";fv,
	 bfMKey(keyID)	'be freeed multikey
 End If

End If
Loop Until fv="q"		
End

Sub bfMKey(ByVal keyID As Integer)
'hard wait until recognised Multikey as unpressed 
	WHILE MULTIKEY(keyID)
			fv=InKey	
	Wend
End Sub
Summary>
Problem occured on single keypress, /double-press I am not using.../
Seems not bug, just slow or speedy response.
Using Multikey for checking different keys pressed is OK, in usual way. Nothing needed.
Using Multikey for checking the SAME key pressed, (e.g. as here Escape for jumping out of For-Next cycles - printing text or drawing charts) goes to troubles.Here MUST be used fix.No needs about knowing anything of value, which Multikey command takes from somewhere.Just hard loop until Multikey itself recognises own status - freeed.
For better readability of code I put this into the routine bfMKey(), aka "be freeed Multikey".
So for real using it will looks like

Code: Select all

       'if scanned different keys
 'If MULTIKEY(&h1) Then  goto labelXYZ   'or
 'If MULTIKEY(fb.SC_ESCAPE) Then  goto labelXYZ   'or	        
keyID=fb.SC_ESCAPE
If MULTIKEY(keyID) Then goto labelXYZ

Code: Select all

       'if scanned the SAME key
keyID=fb.SC_ESCAPE
If MULTIKEY(keyID) Then
       'hard loop added
  bfMKey(keyID)	'be freeed multikey / wait until recognised as unpressed key by Multikey itself /
  goto labelXYZ   'your needed action
End If
'add this to your base routines
Declare Sub bfMKey(ByVal keyID As Integer)
Sub bfMKey(ByVal keyID As Integer)
'hard wait until recognised Multikey as unpressed 
	WHILE MULTIKEY(keyID)
 	   fv=InKey	'clear keybuffer and make some delay
	Wend
End Sub
Note: Maybe documentation update for Multikey ?

Pete
Post Reply