tipforeveryone Posted January 10, 2018 Share Posted January 10, 2018 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: A door can only be opened by pressing F when a electric switch is ON. 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) After turn on the electric switch, the door will be Opened by that "saved" command from Key.F hit. 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() Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 10, 2018 Share Posted January 10, 2018 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 1 Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted January 10, 2018 Author Share Posted January 10, 2018 Thank you Quote Link to comment Share on other sites More sharing options...
Josh Posted January 10, 2018 Share Posted January 10, 2018 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 Quote 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 More sharing options...
tipforeveryone Posted January 10, 2018 Author Share Posted January 10, 2018 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 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 Quote Link to comment Share on other sites More sharing options...
Josh Posted January 10, 2018 Share Posted January 10, 2018 key_a = window:KeyHit(Key.A) if self.condition then... 1 Quote 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 More sharing options...
Recommended Posts
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.