PDA

View Full Version : GetKeyDown and GetKeysDown



mixster
03-24-2008, 02:00 PM
Funnily enough, it's very hard to use IsKeyDown etc. in cases, so I though why not make something that runs through all the keys and stops when it finds a key that is down or in GetKeysDown, all the keys that are down. With these functions, it makes it very easy to use in cases. So, here we go:
function GetKeyDown: Char;
var
i: Integer;
begin
repeat
for i:= 0 to 127 do
if IsKeyDown(Chr(i)) then
begin
Result := Chr(i);
Exit;
end;
until(IsKeyDown(Result))
end;


function GetKeysDown: array of Char;
var
i: Integer;
begin
for i:= 0 to 127 do
if IsKeyDown(Chr(i)) and IsKeyDown(Chr(i)) then
begin
SetArrayLength(Result,High(Result)+2);
Result[High(Result)] := Chr(i);
end;
end;


If you want to test them both (though more GetKeysDown), you can run the following script, and start typing to see the keys come up in the Debug box

program New;
var
i: Integer;
CharsDown: array of Char;

function GetKeyDown: Char;
var
i: Integer;
begin
repeat
for i:= 0 to 127 do
if IsKeyDown(Chr(i)) then
begin
Result := Chr(i);
Exit;
end;
until(IsKeyDown(Result))
end;

function GetKeysDown: array of Char;
var
i: Integer;
begin
for i:= 0 to 127 do
if IsKeyDown(Chr(i)) and IsKeyDown(Chr(i)) then
begin
SetArrayLength(Result,High(Result)+2);
Result[High(Result)] := Chr(i);
end;
end;

begin
repeat
ClearDebug;
CharsDown:= GetKeysDown;
for i:= 0 to High(CharsDown) do
Writeln(CharsDown[i]);
SetArrayLength(CharsDown,0);
Wait(500);
until (GetKeyDown = 'q')
end.

Which will run until you push down the 'q' button.

Enjoy

faster789
03-24-2008, 04:08 PM
kwl!!...probli use this in my sccript but im not shurr...so if any of the key gets pressed down it stops?

mixster
03-24-2008, 04:24 PM
If you're using GetKeyDown, then yes, if using GetKeysDown, then no. What it does is return the key/s that are currently pressed down, so you do
"case KeyDown of
'a': blah;
'b': blah;
etc."

MylesMadness
03-24-2008, 10:20 PM
HOT! I've been needing this for a script you will see soon! Credits for you and a good function for me!

mastaraymond
03-24-2008, 10:34 PM
This would be better:

function GetKeyDown: Char;
var
i: Integer;
begin
for i:= 0 to 127 do
if IsKeyDown(Chr(i)) then
if IsKeyDown(Chr(i)) then
begin
Result := Chr(i);
Exit;
end;
end;

function GetKeysDown: array of Char;
var
i: Integer;
begin
for i:= 0 to 127 do
if IsKeyDown(Chr(i)) then
if IsKeyDown(Chr(i)) then
begin
SetArrayLength(Result,High(Result)+2);
Result[High(Result)] := Chr(i);
end;
end;

Why? There is some kind of "bug" which lets the last pressed button(s) be in some kind of buffer. Meaning that when you pres 'a' and run the script it would writeln A, although you didn't press it..
To "defeat" this bug you should call the procedure twice, I've no clue why it works but somehow it clears the buffer for that char I think.. Try it :rolleyes:

mixster
03-25-2008, 07:21 AM
I tested it with my test script, and it "appeared" to work, so I'll go through it again and check.
Edit: Did a bit more testing and it seems to hold a set of keys pressed from a bit in the buffer and not just the last - I was surprised to see something resembling "until(KeyDown = 'q')" appear in the debug box, so I did a bit more and it does appear to store a few of the keys in the buffer, though it stops after running through the function a few times. I guess as long as the GetKeyDown function isn't the first thing you use, it should be ok, unless you're typing as you do it, though I have tested it with Scar typing functions, but I guess it would work with them too...

mastaraymond
03-25-2008, 12:36 PM
I tested it with my test script, and it "appeared" to work, so I'll go through it again and check.
Edit: Did a bit more testing and it seems to hold a set of keys pressed from a bit in the buffer and not just the last - I was surprised to see something resembling "until(KeyDown = 'q')" appear in the debug box, so I did a bit more and it does appear to store a few of the keys in the buffer, though it stops after running through the function a few times. I guess as long as the GetKeyDown function isn't the first thing you use, it should be ok, unless you're typing as you do it, though I have tested it with Scar typing functions, but I guess it would work with them too...
I know it stores multiple keys, but this "bug" is also during runtime.. Atleast, when I was creating my game it would suddenly jump without pressing the space bar. The prob was that the space stayed in the buffer thats why I called the function twice, to be sure it is being pressed at the moment.. After running the function once, it is "fixed".. Easier fix would be the double If check, you can be 100% sure that the key is really being pressed, that's what we want huh!

Harry
03-25-2008, 12:46 PM
I know it stores multiple keys, but this "bug" is also during runtime.. Atleast, when I was creating my game it would suddenly jump without pressing the space bar. The prob was that the space stayed in the buffer thats why I called the function twice, to be sure it is being pressed at the moment.. After running the function once, it is "fixed".. Easier fix would be the double If check, you can be 100% sure that the key is really being pressed, that's what we want huh!

I realised this too. In my script, before, I would have them hit the C key to stop the script, and it would randomly terminate.

mixster
03-25-2008, 04:02 PM
Edited the original code now to include a double check. The GetKeyDown *should* now work 100% properly and although I didn't do an extra if, I'm sure making GetKeysDown check the same thing twice *should* work the same and run smoothly.