Board index » delphi » Help With Simple Pascal Problem.

Help With Simple Pascal Problem.

Shift Keys and Arrow Keys Problem

I need some code that looks something like this:

If  (LeftShift=Down) or (RightShift=Down) then
Begin
   Ch:=ReadKey;
   If Ch=UpArrow then
   Begin
      ..............................
   End;
End;

Does anyone now how to check if left shift or right shift is down so that it
can be put into this procedure. Any help appreciated.

Thanks.

 

Re:Help With Simple Pascal Problem.


Quote
Andrew Turing wrote:

> Shift Keys and Arrow Keys Problem

> I need some code that looks something like this:

> If  (LeftShift=Down) or (RightShift=Down) then
> Begin
>    Ch:=ReadKey;
>    If Ch=UpArrow then
>    Begin
>       ..............................
>    End;
> End;

> Does anyone now how to check if left shift or right shift is down so that it
> can be put into this procedure. Any help appreciated.

You have to check mem[$40:$17] for it.

const
  rshift=1;
  lshift=2;
  strg=4;
  alt=8;
  scroll=16;
  num=32;
  caps=64;

if (mem[$40:$17] and rshift=rshift) then ...  

HTH

  Wolf

Re:Help With Simple Pascal Problem.


JRS:  In article <lNdx4.6041$Zj2.164...@news2-win.server.ntlworld.com>
of Tue, 7 Mar 2000 20:28:58 in news:comp.lang.pascal.borland, Andrew

Quote
Turing <bhc.tril...@{*word*269}.net> wrote:
>Shift Keys and Arrow Keys Problem

>I need some code that looks something like this:

>If  (LeftShift=Down) or (RightShift=Down) then
>Begin
>   Ch:=ReadKey;
>   If Ch=UpArrow then
>   Begin
>      ..............................
>   End;
>End;

>Does anyone now how to check if left shift or right shift is down so that it
>can be put into this procedure. Any help appreciated.

See TSFAQP item 65.  You don't actually need Ralf's list, since a simple
test program will tell you which bits you need.  You can use set
operations to make the test efficient; maybe  
        if [0,1] <= BitSet(Mem[$40:$17]) then ...   {untested}

However,
$40:$17 on 0..7 order, RS, LS, Ctrl, Alt, SL, CL, NL, Ins set
$40:$18                LC, LA, SReq, Pause, SL, NL, CL, Ins
but I don't recall the non-obvious differences.

It's rather difficult to think of any common question about the TP
language that isn't referred to in TSFAQP.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:Help With Simple Pascal Problem.


Quote
bhc wrote:
> Thank you for your help about the shift keys. The procedure works fine but
> can you put comments on the procedure you posted to explain it. Why do you
> assign the value of 1 to the shift key? Any help would be appreciated.

Please do not send private mail - your question might be interesting for
other users in this NG. Also others might give a better answer than me.

The byte mem[$40:$17] is a status byte.

const
  rshift=1; lshift=2; ctrl=4; alt=8;
  scroll=16; num=32; caps=64; ins=128;

A byte consists of 8 Bits - each bit representating something. To test
if a bit is set you need to "and" the byte with the values above.

Example:
Ctrl and Right-Shift pressed and num lock on would give:
00100101 (=2^0+2^2+2^5=37)
To test if right-shift is pressed:
    00100101 (=rshift or ctrl or num)
and 00000001 (=rshift)
  = 00000001 (=rshift)

if the result is rshift the key is pressed.

Why has rshift the value 1? - I don't know. It's simply a convention.
It's a question compareable to "Why is ``A'' the first letter?".

Wolf

Re:Help With Simple Pascal Problem.


JRS:  In article <38C90D7B.66A33...@gmx.net> of Fri, 10 Mar 2000
15:58:03 in news:comp.lang.pascal.borland, Wolf Behrenhoff

Quote
<wolf.behrenh...@gmx.net> wrote:

>The byte mem[$40:$17] is a status byte.

>const
>  rshift=1; lshift=2; ctrl=4; alt=8;
>  scroll=16; num=32; caps=64; ins=128;

>A byte consists of 8 Bits - each bit representating something. To test
>if a bit is set you need to "and" the byte with the values above.

>Example:
>Ctrl and Right-Shift pressed and num lock on would give:
>00100101 (=2^0+2^2+2^5=37)
>To test if right-shift is pressed:
>    00100101 (=rshift or ctrl or num)
>and 00000001 (=rshift)
>  = 00000001 (=rshift)

>if the result is rshift the key is pressed.

type BS = set of (rshift, lshift, ctrl, alt, scroll, num, caps, ins) ;
var RightShiftPressed, BothShiftsPressed : boolean ;

RightShiftPressed := rshift in BS(mem[$40:$17]) ;
BothShiftsPressed := [rshift, lshift] <= BS(mem[$40:$17]) ;

Once one understands set operations, ISTM that these are clearer to
read; I expect them to generate shorter, faster code.  But I can't at
the moment see an effective way, with sets, to test for at least one
shift pressed ... maybe
ShiftPressed := not ([rshift, lshift] <= BS(not mem[$40:$17])) ;

Of course, for a homework problem, you may need to justify use of sets,
especially if your teacher does not know ...

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:Help With Simple Pascal Problem.


In article <3uzRnKAsFXy4E...@merlyn.demon.co.uk>, Dr John Stockton

Quote
> type BS = set of (rshift, lshift, ctrl, alt, scroll, num, caps,
> ins) ;
> var RightShiftPressed, BothShiftsPressed : boolean ;
> RightShiftPressed := rshift in BS(mem[$40:$17]) ;
> BothShiftsPressed := [rshift, lshift] <= BS(mem[$40:$17]) ;
> But I can't at
> the moment see an effective way, with sets, to test for at least
> one
> shift pressed ... maybe
> ShiftPressed := not ([rshift, lshift] <= BS(not mem[$40:$17])) ;

if (rshift in bs(mem[$40:$17])) or (lshift in bs(mem[$40:$17]))
then write ('a shift key is pressed')

or did I miss something?

Hanford Carr

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

Re:Help With Simple Pascal Problem.


Sorry should have done the following:
type tBS = (rshift, lshift, ctrl, alt, scroll, num, caps,ins);
     BS = set of tBS;

if (rshift in bs(mem[$40:$17])) or (lshift in bs(mem[$40:$17]))
then write ('a shift key is pressed')

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

Re:Help With Simple Pascal Problem.


Quote
Wolf Behrenhoff <wolf.behrenh...@gmx.net> wrote:
> The byte mem[$40:$17] is a status byte.
> const
>   rshift=1; lshift=2; ctrl=4; alt=8;
>   scroll=16; num=32; caps=64; ins=128;
> Why has rshift the value 1? - I don't know. It's simply a convention.
> It's a question compareable to "Why is ``A'' the first letter?".

Not really. It's because that's the order of bits in the byte at $40:$17.
I think this is a little less arbitrary than the order of characters. Of
course you are entitled to see things differently. 8-)

--
______________________________________________________________________
     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
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________

Re:Help With Simple Pascal Problem.


In article <3uzRnKAsFXy4E...@merlyn.demon.co.uk>,
Dr John Stockton  <j...@merlyn.demon.co.uk> wrote:

Quote

>type BS = set of (rshift, lshift, ctrl, alt, scroll, num, caps, ins) ;
>var RightShiftPressed, BothShiftsPressed : boolean ;

>RightShiftPressed := rshift in BS(mem[$40:$17]) ;
>BothShiftsPressed := [rshift, lshift] <= BS(mem[$40:$17]) ;

>Once one understands set operations, ISTM that these are clearer to
>read; I expect them to generate shorter, faster code.  But I can't at
>the moment see an effective way, with sets, to test for at least one
>shift pressed ... maybe
>ShiftPressed := not ([rshift, lshift] <= BS(not mem[$40:$17])) ;

Shiftpressed:=BS(mem[$40:$17])*[rshift,lshift]<>[]

Osmo

Re:Help With Simple Pascal Problem.


Re:Help With Simple Pascal Problem.


Thanks Everyone that helped with this problem.
Your information was very Useful.

A.T.

Re:Help With Simple Pascal Problem.


Re:Help With Simple Pascal Problem.


JRS:  In article <01906254.91aff...@usw-ex0107-049.remarq.com> of Sun,
12 Mar 2000 18:23:38 in news:comp.lang.pascal.borland, handy <handywNOha

Quote
S...@aol.com.invalid> wrote:
>In article <3uzRnKAsFXy4E...@merlyn.demon.co.uk>, Dr John Stockton
>> type BS = set of (rshift, lshift, ctrl, alt, scroll, num, caps,
>> ins) ;
>> var RightShiftPressed, BothShiftsPressed : boolean ;
>> RightShiftPressed := rshift in BS(mem[$40:$17]) ;
>> BothShiftsPressed := [rshift, lshift] <= BS(mem[$40:$17]) ;
>> But I can't at
>> the moment see an effective way, with sets, to test for at least
>> one
>> shift pressed ... maybe
>> ShiftPressed := not ([rshift, lshift] <= BS(not mem[$40:$17])) ;

>if (rshift in bs(mem[$40:$17])) or (lshift in bs(mem[$40:$17]))
>then write ('a shift key is pressed')

>or did I miss something?

The face that "effective" did not perfectly express my meaning?  Yours
must *work*; but it should access $40:17 twice, and looks as if it will
generate longer code.  One is seeking something very readable (presuming
familiarity with sets) that generates code no worse than direct use of
and/or.

Osmo may have it, with
        ShiftPressed := BS(mem[$40:$17])*[rshift,lshift]<>[] ;

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Other Threads