PDA

View Full Version : Simple unblocked readkey.



sixten
16-05-2012, 09:47 PM
function getKey : Smallint;
var ch : Byte;
begin
if keypressed then begin
ch := Ord(readKey);
if ch = 0 then getKey := 1000 + Ord(readKey)
else getKey := ch;
end else getKey := -1;
End;

Pretty smart. I found it online and it works great. Returns -1 if no key pressed. and 1000+scancode or ascii value. It really helps clean up the main program alot!

Super Vegeta
16-05-2012, 11:50 PM
Well, actually, it does return -1 on no key, scancode on keypress, and 1000+scancode on extended keys. Extended keys in crt report themselves as two keys, #0 followed by something else. Arrows are #0#70-something, so they would be returned as 1000+70(something), whereas 'A' would be returned simply as 65.

LP
17-05-2012, 02:26 AM
On Windows you can also use GetAsyncKeyState (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx) to get the state of a specific key using one of VK_[xxx] virtual codes and even detect whether the key has been pressed or not previously.

User137
17-05-2012, 03:40 PM
I would save readKey in a variable, because it is a complicated function? No reason to call it twice.

pitfiend
18-05-2012, 04:59 AM
as far as i remember, readkey removes the key pressed from the buffer. if you press a special key, it will put 2 chars in the buffer: a #0 char and another char. the sequencial call to readkey will catch them. but i think this will not allow to have multiple keypress, as the buffer is very short, only 16 chars (at least in dos/windows i think). unless you manage to extend that buffer.

sixten
30-05-2012, 02:58 AM
I would save readKey in a variable, because it is a complicated function? No reason to call it twice.

It's called a second time to catch the scancode.