Problem with keydown keyhit combo?

Blitz3D Forums/Blitz3D Programming/Problem with keydown keyhit combo?

Guy Fawkes(Posted 2012) [#1]
Hi all. Can someone please tell me why Keydown, combined with keyhit, such as:



can someone please tell me why when i hold control the 1st time, and press the X key, it works just fine. but if i press the X key without the control key, it still activates delete_selected(), and how to fix it?

Any help is greatly appreciated!

Thank You! :)


Matty(Posted 2012) [#2]
KeyHit will give the number of times the key has been pressed since the last time the function was called.

That means that when you hold down keycode 29 or 159 it then calls keyhit(45) which may return a value of greater than zero if you've pressed keycode 45 at any time prior to the function being called.

I'm not sure what functionality you want exactly however so cannot suggest an alternative yet.


Yasha(Posted 2012) [#3]
KeyHit tests if a key has been hit since the last time it was called, or since FlushKeys reset all of the keyboard events. So you can press X somewhere else, which will set X's KeyHit value to 1 (or more, if you press it again), and as soon as you hit Ctrl the second condition will also be met, regardless of whether X is down at that time or not.

You could change to "If (KeyDown(29) Or KeyDown(157)) And KeyDown(45)", which might be better; or even have Ctrl be the KeyHit and only use KeyDown for the X, so that you can have a Sticky-Keys-like effect and hit the modifier key before the action shortcut (as you can set to happen in most Windows apps).


Guy Fawkes(Posted 2012) [#4]
Ok. well i tried flushkeys() already. that didnt work unfortunately...


Yasha(Posted 2012) [#5]
Generally if you need to use FlushKeys in the main loop, your input processing is fatally flawed. Don't do that, that won't help here without a complete redesign of this section.

Try the other suggestion.


Guy Fawkes(Posted 2012) [#6]
Ok. Well I tried sticky keys... that didnt work right... I removed flushkeys... that didnt work right... im at a loss. is there a function where i can combine things like the control key with another key?


Yasha(Posted 2012) [#7]
Did you try using a single If statement with "(KeyDown(29) Or KeyDown(157)) And KeyDown(45)" as the conditional expression?

That's what I meant by "the other suggestion" - I put it in post #3, you didn't comment on whether it worked. It's the most "obvious" solution.


Guy Fawkes(Posted 2012) [#8]
Yea. I tried that too. It didnt do anything...


Yasha(Posted 2012) [#9]
Observe:
While Not KeyDown(1)
	If (KeyDown(29) Or KeyDown(157)) And KeyDown(45)
		Print "Ctrl+X"
	EndIf
	Delay 20
Wend
End


If this condition check doesn't work, the problem is elsewhere in your program.


PowerPC603(Posted 2012) [#10]
This should work fine:
; Set global variables
Global Key_Control, Key_X

; Define graphics window and set backbuffer
Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()

; Before entering the main-loop, flush the keyboard events in case the user pressed some keys before
FlushKeys()

; Main loop
While Not KeyHit(1)
	; Read the required keys into variables
	Key_Control = KeyDown(29) + KeyDown(157)
	Key_X = KeyHit(45)
	; After getting all required keys, flush the keyboard events
	FlushKeys()

	; Clear the screen
	Cls
	; Print something if the CTRL-keys are being pressed down, same for the X-key
	If Key_Control Then Text 10, 10, "Ctrl pressed"
	If Key_X Then Text 10, 20, "X pressed"

	; Only print something if CTRL + X are used at once
	If Key_Control Then
		If Key_X Then
			AppTitle "Changed the program title " + Rand(1, 100)
		EndIf
	EndIf

	; Flip backbuffer to the screen
	Flip
Wend

End



Nexinarus(Posted 2012) [#11]
you could also try
Repeat
	Cls
	Controlkey=(KeyDown(29) Or KeyDown(157))

	mystring$=""
	If KeyDown(45)
		If controlkey=0 mystring$="X"
		If controlkey=1 mystring$="Ctrl + X"
	EndIf

	Text 0,0,mystring$
Until KeyHit(1)

so now if you press [CTRL]+[X} you should see Ctrl+X.
so now if you press [X} you should see X.

I used text as an example test it out. you may understand a bit more. If not... read on.

to solve your problem remove the following
	mystring$=""

		If controlkey=0 mystring$="X"

	Text 0,0,mystring$

and change the following
		If controlkey=1 mystring$="Ctrl + X"

to
		If controlkey=1 delete_selected()

you should now have the following code
Repeat
	Cls
	Controlkey=(KeyDown(29) Or KeyDown(157))

	If KeyDown(45)
		If controlkey=1 delete_selected()
	EndIf

Until KeyHit(1)


this should solve your problem

Last edited 2012