Simulation GetKey() without flush?

Blitz3D Forums/Blitz3D Programming/Simulation GetKey() without flush?

Mr.Waterlily(Posted 2007) [#1]
Hi,

Iīm doing a little textbox for user input using getkey().
And now I want to implement keypress delays and keyrepeats while the user is holding down a button. Is this possible using Getkey()? It flushes the buffer after each read which causes major problems for me.
Thereīs no problem if you use Keydown() for this, but then you get scancodes, and not ascii. And converting them correctly on all languages and all keyboards is probably impossible.

So how can I solve this? Any ideas?

The best way would be to have a parameter to GetKey() that tells if the buffer should be flushed or not, but I guess thatīs up to Mark to implement.. ;)


jfk EO-11110(Posted 2007) [#2]
There's infact a problem with country keyboard settings.

What you can do is: Use GetKey to determine the keys in the correct language. Immediately after getting a key you have to test Keydown in a for 0 to 255 loop to see what (scancode) key was pressed.
Then use your custom keyrepeat stuff, based on millisecs and Keydown.

Frequently check for keydown independently from GetKey as well, to see if eg. a key of the number block was pressed. These keys are not country specific, AFAIK. So you may add support for them without using GetKey.


Mr.Waterlily(Posted 2007) [#3]
Thanks,
Checking for a keydown immediately after the getkey could solve some of the problems.
Iīll try to implement this and see if it can work.
I already use keydown for the numpad and home, end, delete, backspace and the arrows, so it shouldnīt take to long.
I think I need to "remember" if the shift keys or AltGr also are pressed..


big10p(Posted 2007) [#4]
I have some text input code in a project of mine that I could probably rip out for you, if you want?


Mr.Waterlily(Posted 2007) [#5]
big10p: yes please, just email me your code. thanks!

I started doing some GetKey() followed by KeyDown() and it could work, though itīs rather messy(atleast at the moment ;)) code.

If itīs too much trouble to do this, then I might consider dropping keyrepeats for letters. It works for del, backspace and so on which are much more important.
If a user want to type "ggggg", they can press "g" five times.. ;)


big10p(Posted 2007) [#6]
If itīs too much trouble to do this, then I might consider dropping keyrepeats for letters. It works for del, backspace and so on which are much more important.
Actually, having just revisted my code, it doesn't do key repeats for alphanumeric keys, either. It's a tricky problem to get around, I guess it just wasn't worth the effort. :)

Anyway, if you still want it, the code's here:
(sorry about the weird tabs - I use 2-space tabs in my IDE)




Mr.Waterlily(Posted 2007) [#7]
I have done a working keyrepeat function using GetKey in combo with KeyDown, as jfk EO-11110 suggested.
It should work with all languages and keymaps/keyboards since it uses GetKey() to get the ascii code.

And here it is for those of you that want it.
Of course it can be improved. One thing is that it ignores shift and altgr presses for the repeat. These could be easily added if the need arises.

And if you only need keyrepeats for scancodes, just use the KeyRepeats() function and skip the stuff in KeyChecks() except the print lines at the bottom. Itīs pretty generic.

Thanks guys for your help!

-------------------
Global glob_keypressed=False
Global glob_keypressed_timer=0
Global glob_keycheck_ok=True
Global glob_keyrepeat_start=False

Global glob_input_ascii
Global glob_input_scan

;-- Main loop --
Repeat

	KeyChecks()
	KeyRepeats()

Until KeyHit(1)
End
;--
Function KeyChecks()

	Local input_ascii_gk
	Local scan

	If glob_keypressed=False
		input_ascii_gk=GetKey()
		If input_ascii_gk>=32 And input_ascii_gk<>127	;allow ascii above 31, except 127(Del)
			glob_keypressed=True
			glob_input_ascii=input_ascii_gk		
			For scan=0 To 255
				If KeyDown(scan)			
					If scan<>42 Or scan<>54 Or scan<>184	;ignore RShift, LShift, AltGr
						glob_input_scan=scan
						Exit
					End If
				End If
			Next
		Else
			glob_input_ascii=0
		End If
	Else	
		For scan=0 To 255
			If KeyDown(scan)		
				input_scan=scan
				Exit
			End If
		Next
		If input_scan<>glob_input_scan
			glob_keypressed=False
		End If
	End If

	If glob_keypressed=True And glob_keycheck_ok=True
		;-- Do your printing here --
		Print Chr$(glob_input_ascii)
	End If

End Function
;--
Function KeyRepeats()
	If glob_keypressed=True
		If glob_keypressed_timer=0
			glob_keypressed_timer=MilliSecs()
			glob_keycheck_ok=False
		Else
			If (MilliSecs()-glob_keypressed_timer)>600 And glob_keyrepeat_start=False
				glob_keypressed_timer=0
				glob_keycheck_ok=True
				glob_keyrepeat_start=True
			Else If (MilliSecs()-glob_keypressed_timer)>40 And glob_keyrepeat_start=True
				glob_keypressed_timer=0
				glob_keycheck_ok=True
			End If
		End If
	Else
		glob_keypressed_timer=0
		glob_keycheck_ok=True
		glob_keyrepeat_start=False
	End If
End Function



jfk EO-11110(Posted 2007) [#8]
That's looking good. For simplicity you may (as in "don't have to") wrap the entire thing in a function named "CollectKey()" that will return any key in ascii, including cursor keys, backspace, keyrepeat etc. Like the old InKey$.

But this depends on the situation. Guess this one is already working nicely. So thanks for sharing!

BTW you may add it to the code archies.