Pascal input
i know most of you wont have learnt pascal as it is very old and very rarely used but i need some help.
I wish to know how to implement reading of keys such as specific letters or even better the arrow keys, i.e using up and down to move in a menu
i know that im supposed to be using READKEY but have no idea how
What about me? What about Raven?
program whatever;uses crt;var myspiffykeypress:char;beginmyspiffykeypress := readkey;if (myspiffykeypress = "c") then {do stuff}if (myspiffykeypress = chr(27)) then {do more stuff{ end.
NOTE: I'm not sure what chr(27) actually is, so i'd suggest putting your own number in there...
Here's a little program that prints out the code of the key you are pressing... (Hold ALT and press 0169 to exit)
program whattheheckisthecode;uses crt;var myspiffykeypress:charbeginmyspiffykeypress:=' ';while (myspiffykeypress <> '©') dobegin myspiffykeypress:=readkey; writeln (myspiffykeypress,':',ord(myspiffykeypress));end;end.
That should work...
Edited by - tsu on March 11, 2002 1:56:11 PM
Edited by - tsu on March 11, 2002 2:01:17 PM
thx, so do the arrow keys work in a similar way, do they just have a certain code i use or something and how do i use it?
What about me? What about Raven?
What about me? What about Raven?
Yes, every key has it''s own code, and so you just need to find it...
the arrow keys are odd, though, because their numbers arent in order (like ''b'' would come after ''a''), so i can never remember them... (i dont know them offhand, i think they''re in the 70''s... or the 20s...i''m not sure)
when you assign a variable to ''readkey'', the character is stored in that variable, so use chr() and ord() to convert between them...
i also found that when you just have var1:=readkey, it waits for you to enter a key, so i usually put:
the arrow keys are odd, though, because their numbers arent in order (like ''b'' would come after ''a''), so i can never remember them... (i dont know them offhand, i think they''re in the 70''s... or the 20s...i''m not sure)
when you assign a variable to ''readkey'', the character is stored in that variable, so use chr() and ord() to convert between them...
i also found that when you just have var1:=readkey, it waits for you to enter a key, so i usually put:
if (keypressed) then begin {a bunch of if-statements for each key} end;
The arrowkeys (along with some of the other odd keys) have special extended keycodes. Try this:
If I remember correctly the arrow keys are ''M'' ''K'' ''P'' ''O'' (or something like that.
You might also want this:
To allow realtime action instead of waiting for the user to hit a key
uses crt;var C:Char;begin C:=ReadKey; if C=#0 then begin //Thats chr(0) C:=ReadKey; //Theres already another ''key'' in the buffer Writeln(''Extended key ''+C); end else Writeln(''Key ''+C);end;
If I remember correctly the arrow keys are ''M'' ''K'' ''P'' ''O'' (or something like that.
You might also want this:
repeat if KeyPressed then begin C:=Readkey; DoSomethingWithKey; end; DoGameAction;until gameend;
To allow realtime action instead of waiting for the user to hit a key
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement