Jump to content

KeyHit Problem


tipforeveryone
 Share

Recommended Posts

Hi,

There is something not right with window:KeyHit() function, I did mention this problem in the past but wasn't solved

This is the code

Script.condition = false

function Script:UpdateWorld()
	if self.condition then
		if window:KeyHit(Key.F) then
  			System:Print("F key pressed!!")
		end
	end
	if window:KeyHit(Key.L) then
		self.condition = true
	end
end

Process:

  • self.condition = false will make sure that when I press the F key, there is nothing happen
  • then If I press the L key to make self.condition = true, the console displays string "F key pressed!".

I suppose the window:KeyHit(Key.F)  is somehow  "saved" and waits until the condition is met to execute code inside it.

This is annoying in some situations indeed. Here is an example:

  1. A door can only be opened by pressing F when a electric switch is ON.
  2. Player may try to press the F key to open door before he is instructed that the switch must be ON to open the door (at this point the window:KeyHit(Key.F) is "saved" as I said above)
  3. After turn on the electric switch, the door will be Opened by that "saved" command from Key.F hit.
  4. I don't want that. It should be kept closing until I press F with the turned ON electric switch.

The temporary fix is using window:KeyDown() with a boolean variable like this

Script.keyPressed = false

function Script:UpdateWorld()
	if window:KeyDown(Key.F) then
		if self.keyPressed == false then
			self.condition = true
			--ANOTHER CODE

			self.keyPressed = true
		end
	else
		self.keyPressed = false
	end
end

But this causes another problem, that is: If I replace a function to the "--ANOTHER CODE", will be executed many times because it is in UpdateWorld() function. Therefore I don't want to use this fix

So how can I execute my code only one time when I press my desired key without getting the first problem with window:KeyHit()

Link to comment
Share on other sites

Flush the keys to get the desired effect: https://www.leadwerks.com/learn?page=API-Reference_Object_Window_FlushKeys. Also be aware of Keyhit for the same key being stored in a single frame. That means of 2 frames us KeyHit for the Spacebar, only one script will have the spacebar set to true(pressed).

*edit: just read the topic you were refering to. So this solution might not work for you. https://www.leadwerks.com/community/topic/15185-windowkeyhit-strange-problem/?tab=comments#comment-102329

  • Thanks 1
Link to comment
Share on other sites

Shouldn't it look like this?:

Script.condition = false

function Script:UpdateWorld()
	if window:KeyHit(Key.F) then
  		if self.condition then
  			System:Print("F key pressed!!")
		end
	end
	if window:KeyHit(Key.L) then
		self.condition = true
	end
end

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Sometime I need something like this

if self.condition then
	if window:KeyHit(Key.A) then
	--Do a bunch of code--
	end
	if window:KeyHit(Key.B) then
	--Do a bunch of code--
	end
	if window:KeyHit(Key.C) then
	--Do a bunch of code--
	end
end

It is much clear and structurable than this :D I think

if window:KeyHit(Key.A) then
	if self.condition then
	--Do a bunch of code--
	end
end
if window:KeyHit(Key.B) then
	if self.condition then
	--Do a bunch of code--
	end
end
if window:KeyHit(Key.C) then
	if self.condition then
	--Do a bunch of code--
	end
end

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...