read cursor keys 
Author Message
 read cursor keys

Quote:
> I have read the FAQs, and I still do not know how to read cursor keys.
> How do I ... ?
> 1. read cursor key
> 2. if cursor key pressed, so something
> Any *truly* helpful/readily applicable examples?

   Professor Salmi's TPFAQ covers this: #131.  However, since you can't
seem to "find" this, here's how you do it:

Uses CRT;
Var C1,C2 : char;
...
  C1 := ReadKey;
  case UpCase(ReadKey) of
   #00 : begin  { special/extended (cursor) keystroke }
           C2 := ReadKey;        { read the scan code }
           case C2 of
             #71 : do_Home;
             #80 : do_Dn_Arrow;
             etc.
            else
           end
          end;
   'Q',#27 : quit_process;
   else
  end



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys

Quote:

>Some do it like this:

>-------------begin------------------

>uses crt;

>var
>ch: char;

>begin

>repeat

>ch:=ReadKey; if ch=#0 then ch:=char(128+byte(ReadKey));

Bad bad bad. What if the user presses keys that produce characters in
range 128..255. They will be interpreted as control keys.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys
On Sat 10 Jun 2000 12:36:08a,  In article

Quote:

>> I have read the FAQs, and I still do not know how to read cursor keys.
>> How do I ... ?
>> 1. read cursor key
>> 2. if cursor key pressed, so something
>> Any *truly* helpful/readily applicable examples?

>   Professor Salmi's TPFAQ covers this: #131.  However, since you can't
>seem to "find" this, here's how you do it:

>Uses CRT;
>Var C1,C2 : char;
>...
>  C1 := ReadKey;
>  case UpCase(ReadKey) of
>   #00 : begin  { special/extended (cursor) keystroke }
>           C2 := ReadKey;        { read the scan code }
>           case C2 of
>             #71 : do_Home;
>             #80 : do_Dn_Arrow;
>             etc.
>            else
>           end
>          end;
>   'Q',#27 : quit_process;
>   else
>  end

Some do it like this:

-------------begin------------------

uses crt;

var
ch: char;

begin

repeat

ch:=ReadKey; if ch=#0 then ch:=char(128+byte(ReadKey));

case ch of
#27: {ESC key} halt;
#200: writeln('up arrow');
#208: writeln('down arrow');
#205: writeln('right arrow');
#203: writeln('left arrow');
#32: writeln('spacebar');
else {do nothing}
end {case};

until false;

end.

------------------end------------------

To find the code for any key, use this small
program:

--------------begin-----------------

uses crt;

var
ch: char;

begin

repeat
ch:=ReadKey; if ch=#0 then ch:=char(128+byte(ReadKey));
write('#',byte(ch):4);
until ch=#27;

end.

--------------end---------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys


Quote:
> On Sun 11 Jun 2000 11:39:28a,  In article



> >> >1. you should at least replace the Write by a WriteLn

> >> Umm... Why?
> >well, just because you give your output without any spaces

> you obviously didn't run the program, did you?

Stuart,
to be honest, I did run it, I even ran both sources you gave in your
posting from:
Sat 10 Jun 2000 12:36:08 (header from my newsreader - times may be
different)

The first piece of code didn't run well at all :

Quote:
> Some do it like this:
> -------------begin------------------
> uses crt;
> var
> ch: char;
> begin
> repeat
> ch:=ReadKey; if ch=#0 then ch:=char(128+byte(ReadKey));
> case ch of
> #27: {ESC key} halt;
> #200: writeln('up arrow');
> #208: writeln('down arrow');
> #205: writeln('right arrow');
> #203: writeln('left arrow');
> #32: writeln('spacebar');
> else {do nothing}
> end {case};
> until false;
> end.
> ------------------end------------------

because in the "else" part (where all other keystrokes should be
treated) nothing is done

The second piece of code, the one I'm referring to:

Quote:
> --------------begin-----------------
> uses crt;
> var ch: char;
> begin
> repeat
> ch:=ReadKey; if ch=#0 then ch:=char(128+byte(ReadKey));
> write('#',byte(ch):4);
> until ch=#27;
> end.
> --------------end---------------------

You are indeed "writing" one or two spaces between the # and the value
due to the fact that you are giving a length of 4 to the output
result,
but when hitting a next key the # is written immediately next to the
result of the previous key:
an example:
when hitting the keys F1,F2,F3,F4 and Enter your program gives:
# 187# 188# 189# 190#  13, leaving no spaces between the results.
OK, I know the "user interface" in this case is of minor interest and
is not really necessary, but I just like to see a neatly formatted
output.

BTW: I participated in an interesting tread concerning *Reading
function/cursor keys* launched by James O'Gorman.
Especially the answer of Osmo Ronkanen could be of interest to this
thread.
CU
Marcel
--
Please remove the *.NoSpam* entry from my eMail address when replying.
It's meant to keep Spam and Advertising out, not friends.
Please test my new web pages
http://users.pandora.be/marcel.van.der.mierde/
still under heavy construction



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys

Quote:
>    I don't think so.  It's long, convoluted, and error prone (having so
> much code)...and it won't work! (major bug...)  It's probably a good deal
> slower in execution than this, too:

>    Of course, there are a number of things which _should_ be done to mine
> (and yours), since many keys aren't processed and many others should be
> processed uniquely.  It all depends on what you're trying to accomplish
> with any "keystroke code", and I wouldn't actually _use_ my code - I just
> wanted to show you a better (and correct) way of handling yours... 8<}}

--

Thanks for your response, but the program I offered works fine on my
computer.  Could you be more specific regarding the 'major bug'.  As far as
speed is concerned, on my machine when I press a key or a combination of
keys, the intended response is on the screen instantly.  Speed is not a
concern with such a simple program.

The question that started this thread was:

I have read the FAQs, and I still do not know how to read cursor keys.
How do I ... ?
1. read cursor key
2. if cursor key pressed, so something
Any *truly* helpful/readily applicable examples?

What my program offered was a way to read all key inputs, including the
cursor keys and any combination using ALT, CTRL, and SHIFT.  I think I
succeeded in helping with that, and if I understand your use of
'convoluted', I'd have to disagree - the code is straight forward, and self
explanatory.

Bill Vergos



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys

Quote:
> >    I don't think so.  It's long, convoluted, and error prone (having so
> > much code)...and it won't work! (major bug...)  It's probably a good deal
> > slower in execution than this, too:
> >    Of course, there are a number of things which _should_ be done to mine
> > (and yours), since many keys aren't processed and many others should be
> > processed uniquely.  It all depends on what you're trying to accomplish
> > with any "keystroke code", and I wouldn't actually _use_ my code - I just
> > wanted to show you a better (and correct) way of handling yours... 8<}}

> Thanks for your response, but the program I offered works fine on my
> computer.  Could you be more specific regarding the 'major bug'.  

   Yes: you had a While loop which controlled the whole programs logic,
but you didn't assure that the While loop was entered - by doing a
ReadKey to load Ch before the While statement.  This is a fundamental
error in Pascal, because you can't depend on a default value for a
variable which hasn't been defined (explicitly given a value).
   I suspect the program may work for you because you're running it in
the IDE.  The TP/BP IDE initializes all declared data (Vars) - a
"feature" I strongly disagree with - but it's a subversion of the Pascal
language: the value of all variables are _undefined_ until some explicit
action assigns a value to them.  Thus, if you package your program as a
.EXE and attempt to run it, it may or may not work.  Since Pascal
variables are _undefined_ at program startup, Ch could very well have a
value which will skip/bypass the whole loop.
   That's a "major bug" in my view, and I'm sure you'd agree if you ever
had to debug it...

Quote:
> As far as
> speed is concerned, on my machine when I press a key or a combination of
> keys, the intended response is on the screen instantly.  Speed is not a
> concern with such a simple program.

   Perhaps not, but a huge line of "if" statements, nested or serial,
implies less than optimal knowledge and use of the language, and the
generation of more object code, whether there are bugs in it or not, will
run slower and cause more problems (in program size, maintenance,
enhancements, etc.).  
   You posted you code and asked "how's this", and I responded with
several serious pointers and thoughts.  You have every right to ignore
them (and invite problems as you attempt to "develop" a programming
career) or you can take a good look at what you did and how I tried to
improve it...and possibly learn from it.  It's entirely up to you.

Quote:
> The question that started this thread was:

> I have read the FAQs, and I still do not know how to read cursor keys.
> How do I ... ?
> 1. read cursor key
> 2. if cursor key pressed, so something
> Any *truly* helpful/readily applicable examples?

> What my program offered was a way to read all key inputs, including the
> cursor keys and any combination using ALT, CTRL, and SHIFT.  I think I
> succeeded in helping with that, and if I understand your use of
> 'convoluted', I'd have to disagree - the code is straight forward, and self
> explanatory.

   I honestly consider the overblown series of "if" statements and the
inherently complex issue such code presents _not_ "straight forward": it
was hard to read, replete with excess code (all the "Ord" calls, when
that work could have been done once, at the start of the loop, etc.), and
quite hard to debug.  I offered different code (a Case statement) which
provided all your functionality - in simpler, easier to read, and concise
code - to show you and others how it could be improved.
   If you're enamored with your code/style, that's certainly your "deal",
but most experienced programmers are usually appreciative of what they
find that improves their skills or eases their work.  
   Again, your choice...


Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys

Quote:

> On Sun 11 Jun 2000 03:21:42a,  In article




>>>Are the keyboards in your country mapped differently than they are here
>>>in the US?
>>Of course they are.
> Why "Of course".  Why not just "Yes".

Perhaps it should be fairly obvious that Finland uses a wider variety of
characters?

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will

______________________________|_______________________________________



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys

Quote:
>>>>ch:=ReadKey; if ch=#0 then ch:=char(128+byte(ReadKey));

>>>Bad bad bad. What if the user presses keys that produce characters in
>>>range 128..255. They will be interpreted as control keys.

>With some changes of type, one might add 256 instead of 128.

Better even is to shift by 8 ( *256)
This will give room for other odd things (e.g. combining with state of
win/shift/ctrl/alt) in the future.

Both (+256 or *256) will however change the type of the character being
processed from char to integer.

function getkey():integer;

var key : integer; { avoid recursion problems, when using getkey}

begin
 key:=ord(readkey);
 if key=0 then key:=ord (readkey) shl 8;
 {other special detections can be added here in the future}
 Getkey:=key;
end;

Quote:
>>Are the keyboards in your country mapped differently than they are here in
>>the US?

>In the back of a proper DOS manual, such as that for DOS 5.0, are
>diagrams for a score of different keyboards.  AFAICS, only that for the
>USA is handicapped in being unable to produce even one character above
>#127 with a single [shifted] stroke.  The general ignorance, among the
>youth of the USA, about the rest of the world is indeed lamentable;
>though not confined to the rest of the world, since many of your own
>Hispanics will benefit from a Portuguese, Spanish, or Latin American
>keyboard.

In the NL, US keyboards are also quite common. (accents, both own, as German
and French for borrowed words are used, but are quite rare), so it saves a
lot of codepage troubles.


Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys


Quote:
>>>>>ch:=ReadKey; if ch=#0 then ch:=char(128+byte(ReadKey));

>>>>Bad bad bad. What if the user presses keys that produce characters in
>>>>range 128..255. They will be interpreted as control keys.

>>With some changes of type, one might add 256 instead of 128.

>Better even is to shift by 8 ( *256)
>This will give room for other odd things (e.g. combining with state of
>win/shift/ctrl/alt) in the future.

>Both (+256 or *256) will however change the type of the character being
>processed from char to integer.

No, integer is not enough. One must use word.

Quote:

>function getkey():integer;

>var key : integer; { avoid recursion problems, when using getkey}

>begin
> key:=ord(readkey);
> if key=0 then key:=ord (readkey) shl 8;

If readkey gives code greater than 128 the assignment will give range
check error.

Quote:
> {other special detections can be added here in the future}
> Getkey:=key;
>end;

That method is nice as one can still use chr(lo(key)) for the character
value. If one added 256 then it would not work. The adding method on the
other hand is better of one wants to use the key as index into an array.

Quote:

>In the NL, US keyboards are also quite common. (accents, both own, as German
>and French for borrowed words are used, but are quite rare), so it saves a
>lot of codepage troubles.

There are no code page troubles unless one wants to write Danish or
Norwegian or some other {*filter*} language. The standard PC codepage
supports most West European languages.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys

Quote:

>>Better even is to shift by 8 ( *256)
>>This will give room for other odd things (e.g. combining with state of
>>win/shift/ctrl/alt) in the future.

>>Both (+256 or *256) will however change the type of the character being
>>processed from char to integer.

>No, integer is not enough. One must use word.

Yup, you are right. To my defence, I downgraded a FPC routine
here on the fly. (which used longint)

Quote:

>That method is nice as one can still use chr(lo(key)) for the character
>value.

That, and the possibility to use more special codes (beside 0) later were the
big advantages. However I use and 255, since on some compilers (including FPC)  
lo(longint) returns a the lower WORD, which could cause problems.

Quote:
> If one added 256 then it would not work. The adding method on the
>other hand is better of one wants to use the key as index into an array.
>>In the NL, US keyboards are also quite common. (accents, both own, as German
>>and French for borrowed words are used, but are quite rare), so it saves a
>>lot of codepage troubles.

>There are no code page troubles unless one wants to write Danish or
>Norwegian or some other {*filter*} language. The standard PC codepage
>supports most West European languages.

Strictly yes, but I include keyboard drivers too.


Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys

Quote:


>> On Sun 11 Jun 2000 03:21:42a,  In article




>>>>Are the keyboards in your country mapped differently than they are here
>>>>in the US?
>>>Of course they are.
>> Why "Of course".  Why not just "Yes".

>Perhaps it should be fairly obvious that Finland uses a wider variety of
>characters?

Perhaps instead it should be fairly obvious that less than 0.01% of the
population of this planet knows (or cares for that matter) how wide the variety
of characters is in Finnish.

I think the issue was one of civility and/or inappropriate nationalistic
pride.

Frankly, my dear, I don't give a damn.

Wait... that was a different Scarlet. :-)

---
Fifty dollars please.



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys

Quote:
>    Yes: you had a While loop which controlled the whole programs logic,
> but you didn't assure that the While loop was entered - by doing a
> ReadKey to load Ch before the While statement.  This is a fundamental
> error in Pascal, because you can't depend on a default value for a
> variable which hasn't been defined (explicitly given a value).

Sorry, but this makes no sense to me.  Ch was declared a CHAR variable from
the start, and if you read the first 3 lines of the code found at the bottom
and you'll see that ch is passed a character, and if the first ord(ch) is 0,
then a second is again read by Ch.  You have referred to errors several
times, and I'll say it again, the program ran error free.

Quote:

>    I honestly consider the overblown series of "if" statements and the
> inherently complex issue such code presents _not_ "straight forward": it
> was hard to read, replete with excess code (all the "Ord" calls, when
> that work could have been done once, at the start of the loop, etc.), and
> quite hard to debug.  I offered different code (a Case statement) which
> provided all your functionality - in simpler, easier to read, and concise
> code - to show you and others how it could be improved.

You seem to forget that the original post from Robert J.N. Speirs asked for
a "Any *truly* helpful/readily applicable examples?"  The tone of that
implies a novice to programming, and I thought what clearer way could there
be than to present the basic logic condition:  "If...then".  Your code may
be easier to read by some, but not by beginners. You are right about the so
many "Ord" calls, but there was no need to 'debug' the program.

Quote:
> You posted you code and asked "how's this", and I responded with
> several serious pointers and thoughts.  You have every right to ignore
> them (and invite problems as you attempt to "develop" a programming
> career) or you can take a good look at what you did and how I tried to
> improve it...and possibly learn from it.  It's entirely up to you.

If you know how to go about copying the exact code you offered to a PAS
file, then do so and you will notice that it has glaring errors.  You
offered one pointer that I adopted -  'Case .. of'.  Aside from that, your
thoughts seem to be of a pejorative nature, so they were ignored.  This is
just a guess, but I'll bet I've been programming longer than you have been
alive.  Turbo Pascal is the last language learned, and was picked up to fend
off boredom after retirement.  The progression, should you be interested, is
BASIC, GWBASIC, QBASIC, QBX (compiler BASIC), - retirement -, TURBO PASCAL
7.0.  My career had very little to do with computer programming.  It was a
self-taught skill acquired when our group (circa 1975) determined that
self-scoring. "press one button", assessment tools were not only accurate,
but saved valuable time.  Our concern was never "can we make this 125 bytes
smaller, or can we speed this up by 265 milliseconds".  Our concern was only
that the graphic presentation be visually appealing, the measurements
accurate, and that the profile generated accurately reflect the individual.

I offer the following code and you "...have every right to ignore (the code)
and invite problems as you attempt to "develop" a programming career)    -
{a bit pejorative, would you not agree}

while ch<>#27 do
begin
  ch:=readkey;
if ord(ch)<>0 then  writeln('(',ord(ch),') = ',ch)
  else
   begin
      write('(',ord(ch),'+');
      ch:=readkey;
      n:=ord(ch);
      case ch of
      #49 : writeln(n,') = alt+n');
      #50 : writeln(n,') = alt+m');
      #59..#68 : writeln(n,') = F',n-58);
      {up,left,right,down Cursor are procedures}
      {which are not incuded here}
      #72 : upCursor;
      #75 : leftCursor;
      #77 : rightCursor;
      #80 : downCursor;
      #84..#93 : writeln(n,') = SHIFT+F',n-83);
      #94..#103 : writeln(n,') = CTRL+F',n-93);
      #104..#113 : writeln(n,') = ALT+F',n-103);
      #115 : Writeln(n,') = CTRL+left cursor');
      #116 : Writeln(n,') = CTRL+right cursor');
      else
      writeln(n,') = ',ch);
    end;
    end;
  end;
end.

Cheers,

Bill Vergos



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys
:population of this planet knows (or cares for that matter) how wide the variety
:of characters is in Finnish.

:Frankly, my dear, I don't give a damn.

Attaboy. Fair enough not to expect you to utilize any services
around here, right?

   All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa

Timo's  FAQ  materials  at   http://www.uwasa.fi/~ts/http/tsfaq.html



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys


Quote:


> >    Yes: you had a While loop which controlled the whole programs logic,
> > but you didn't assure that the While loop was entered - by doing a
> > ReadKey to load Ch before the While statement.  This is a fundamental
> > error in Pascal, because you can't depend on a default value for a
> > variable which hasn't been defined (explicitly given a value).

> Sorry, but this makes no sense to me.  Ch was declared a CHAR variable
from
> the start, and if you read the first 3 lines of the code found at the
bottom
> and you'll see that ch is passed a character, and if the first ord(ch) is
0,
> then a second is again read by Ch.  You have referred to errors several
> times, and I'll say it again, the program ran error free.

One error. And it's a tricky one because the program will normally run
without a problem.

The problem is that in Pascal, the value of a variable is not defined until
you set it to something. So if you have

Var
  ch:Char;
Begin
  Writeln(ord(ch));
End.

you could get any value from 0 to 255. One or more versions of TurboPascal
initialises global variables to 0 but this is not true of all versions of TP
on all platforms. If you want to rely on this behaviour, you would have to
make sure the code will only be compiled by that compiler, and that the
variable will be global. No version will initialise local variables in a
procedure or function because this is not possible without slowing down all
programs. And having all your variables global makes a program hard to read
and understand, unless the program is very small.

Now, the code you posted started:

uses crt;
var  ch:char;
begin
clrscr;
writeln('Press <ESC> to stop');
writeln;
writeln('Press one key or any combination of keys...');
writeln;
while ch<>#27 do
begin

You don't initialise ch until after the value of ch has been checked by
"While".

This will normally work. If the value of ch is random, you have only 1 in
256 chance of seeing the error. And the value is not normally random. It's
just whatever happens to be in that byte of memory. If you are using a
particular compiler  it might initialise all _global_ variables to 0. The
IDE may initialise the memory to 0. A previous program may have set the
value. If the variable is a local one in a procedure, the value depends on
what procedures you have called before this one.

What this means is that it's a worse bug than one which throws the program
out every time, because it's more difficult to track down. If you don't know
about uninitialised variables it's very hard to see.

The way to fix it is to make sure that all variables are initialised before
their value is used. You can take your pick of

ch := #0;
while (ch<>#27) do
Begin
  ch := ReadKey; { at the start of the loop }
  blablabla;
End;

ch := Readkey;
while (ch<>#27) do
Begin
  blablabla;
  ch := ReadKey; { note - at the end of the loop }
End;

repeat
  ch := ReadKey;
  blablabla;
until ch=#27;

(Personally, I think the last is most readable.)

Hope that's clearer now.

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 read cursor keys

Quote:


>:population of this planet knows (or cares for that matter) how wide the variety
>:of characters is in Finnish.

>:Frankly, my dear, I don't give a damn.

>Attaboy. Fair enough not to expect you to utilize any services
>around here, right?

I have no idea what you're talking about.  Please elaborate.

Quote:
> All the best, Timo

Fifty dollars please.


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. read cursor key

2. Reading function/cursor keys

3. Source Code For Using Function Keys or Cursor Keys - Help :)

4. Source Code For Using Function Keys or Cursor Keys - H

5. How to put cursor-keys in keyboard buffer??

6. !!! Help, Cursor Keys

7. How control Cursor-Keys with TPW 1.5 in a WinCRT-Window

8. How control Cursor-Keys with TPW 1.5 in a WinCRT-Window

9. Cursor Keys

10. How to get cursor key events ?

11. Blinking cursor in read-only DBMemo

12. Q: Reading cursor position in TDBGrid

 

 
Powered by phpBB® Forum Software