Getch interfering with Input.KeyDown, how can I use Input.KeyDown to read what key is being pressed?
Author |
Message |
GreatPumpkin
|
Posted: Tue Dec 31, 2013 9:32 pm Post subject: Getch interfering with Input.KeyDown, how can I use Input.KeyDown to read what key is being pressed? |
|
|
What is it you are trying to achieve?
I'm running into a problem trying to use Input.KeyDown with getch. The problem I'm running into didn't occur when I had everything running over and over again together in one loop without being stopped, but I'm trying to break it up into turn phases within the main loop. I know I should find the source of the problem, but I've had problems with using getch already and I knew I'd have to replace it, I want to eventually replace it with a mouse script to select the units and cities in my game, but for now I just want to switch to Input.KeyDown.
Every time I've used Input.KeyDown it's been to move objects in an if statement like this:
code: | if key ('a') and tiles (Array ((unitInfo (selectedUnit + selectedCity * 10, 2) - 1), (unitInfo (selectedUnit + selectedCity * 10, 3)))) = 1 %%%%IF TILE IS LAND
and units (Array ((unitInfo (selectedUnit + selectedCity * 10, 2) - 1), (unitInfo (selectedUnit + selectedCity * 10, 3)))) = 0 then %%%%AND IF SPACE NOT OCCUPIED THEN
units (Array ((unitInfo (selectedUnit + selectedCity * 10, 2)), (unitInfo (selectedUnit + selectedCity * 10, 3)))) := 0 %%%%REMOVE UNIT FROM unitFILE
unitInfo (selectedUnit + selectedCity * 10, 2) -= 1 %%%%CHANGE UNIT INFO
%end if
elsif key ('d') and tiles (Array ((unitInfo (selectedUnit + selectedCity * 10, 2) + 1), (unitInfo (selectedUnit + selectedCity * 10, 3)))) = 1
and units (Array ((unitInfo (selectedUnit + selectedCity * 10, 2) + 1), (unitInfo (selectedUnit + selectedCity * 10, 3)))) = 0 then
units (Array ((unitInfo (selectedUnit + selectedCity * 10, 2)), (unitInfo (selectedUnit + selectedCity * 10, 3)))) := 0
unitInfo (selectedUnit + selectedCity * 10, 2) += 1
%end if
end if |
All the possible key presses are already defined in the script, I'd like to read the key that is being pressed, and store that information, without comparing it to an "w" or a "1" or "2", etc.
I'd like to learn how to use it like getch in this instance :
code: | proc selUnit
Font.Draw (("Select unit to activate."), 620, 40, font3, red)
locatexy (615, 45)
getch (c) %%%%%TO BE REPLACED WITH MOUSE SELECTOR
put strint (c)
selectedUnit := strint (c)
end selUnit |
What is the problem you are having?
The problem i don't really understand "var key : array char of boolean" other than that it's a boolean, not a string or integer. I'd like selectedUnit to equal whatever number key (1-8) that is being pressed.
The problem with getch is that when I go to press w,a,s,d or any other non numeral character, then use getch, it reads illegal character passed to strint, because you can't numberfy a w. I could write into the script all 8 possible keypresses to use Input.KeyDown, but that seems unnecessary, there's gotta be a smarter way to receive input during loops. I know theres get, but the user has to press enter, but that doesn't make much sense, because there'll be only 8 units max per city, so the information should be stored the second the player hits the corresponding key
Describe what you have tried to solve this problem
<Answer Here>
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
For now I've switched to get.
Turing: |
Above
<Add your code here>
|
Please specify what version of Turing you are using
<Answer Here> |
|
|
|
|
|
Sponsor Sponsor
|
|
|
GreatPumpkin
|
Posted: Tue Dec 31, 2013 9:43 pm Post subject: RE:Getch interfering with Input.KeyDown, how can I use Input.KeyDown to read what key is being pressed? |
|
|
A second problem has occurred with the get selectedUnit, which is pretty much the exact same as the one that occurred with the getch. It records all the w,a,s,d presses when the "get selectedUnit" isn't even running. So I press w,a,s,d then press the button thatrunso "get selectedUnit" and w,a,s,d is already in the field. Any help? |
|
|
|
|
|
Dreadnought
|
Posted: Wed Jan 01, 2014 12:42 am Post subject: Re: Getch interfering with Input.KeyDown, how can I use Input.KeyDown to read what key is being pressed? |
|
|
If I follow correctly, you're using getch after a pause and you are having trouble dealing with keypresses that are buffered (user hits 'w' during the pause and that is what getch eventually returns). Is this correct?
GreatPumpkin wrote:
The problem i don't really understand "var key : array char of boolean" other than that it's a boolean, not a string or integer.
It is an array of boolean (true/false) which has characters for indices. The way it works is that every character is actually a number (from 0 to 255) and so you end up with something that looks like
Turing: | var chars : array 0 .. 255 of boolean |
If you want to know the numerical equivalent to a character see the prd function (or look up an ascii table).
GreatPumpkin wrote: I'd like selectedUnit to equal whatever number key (1-8) that is being pressed.
The problem with getch is that when I go to press w,a,s,d or any other non numeral character, then use getch, it reads illegal character passed to strint, because you can't numberfy a w. I could write into the script all 8 possible keypresses to use Input.KeyDown, but that seems unnecessary, there's gotta be a smarter way to receive input during loops. I know theres get, but the user has to press enter, but that doesn't make much sense, because there'll be only 8 units max per city, so the information should be stored the second the player hits the corresponding key
Well, we can check if the character returned by getch is one of '1', '2' , ... , '8' if so save the value if not get another character.
How do we get the value? Well, like I said above every character has a numerical equivalent between 0 and 255. Conveniently, the values for consecutive numbers are also consecutive. (again see ord
For example ord('0') = 48, ord('1') = 49, ... ,ord('9') = 57 So if the value is not between the values of '1' and '8' you know its not what you want.
Another thing you might find helpful is Input.Flush (and any other functions in the input module). This function flushes (clears) the key buffer, so that all those keys that getch is waiting to return disappear. Placing this at the end of a pause can cause key presses during the pause to be ignored. (I think this might help you resolve the problem you mentioned in the second post.
Hope this helps! |
|
|
|
|
|
Raknarg
|
Posted: Wed Jan 01, 2014 1:37 am Post subject: RE:Getch interfering with Input.KeyDown, how can I use Input.KeyDown to read what key is being pressed? |
|
|
Quote: If you want to know the numerical equivalent to a character see the prd function (or look up an ascii table).
You meant ord, not prd |
|
|
|
|
|
Dreadnought
|
Posted: Wed Jan 01, 2014 2:35 pm Post subject: Re: Getch interfering with Input.KeyDown, how can I use Input.KeyDown to read what key is being pressed? |
|
|
Yes, thanks Raknarg |
|
|
|
|
|
Raknarg
|
Posted: Wed Jan 01, 2014 3:50 pm Post subject: RE:Getch interfering with Input.KeyDown, how can I use Input.KeyDown to read what key is being pressed? |
|
|
If he wanted to make sure getch didn't interfere with Input.KeyDown, he could just make a hasch clause that only activates getch when a key is pressed |
|
|
|
|
|
|
|