Board index » delphi » Bad code in handling keys?

Bad code in handling keys?

Hi,
The following code should respond only to Control,Shift and an Fkey,
then adjust the level variable.
It seems awkward to me, but I do not know a better way.

if (ssCtrl in Shift) and (ssShift in Shift) and (Key in
VK_F1..VK_F12])
  then begin
   case Key of
    VK_F1  : Level:=1;
    VK_F2  : Level:=2;
    VK_F3  : Level:=3;
    VK_F4  : Level:=4;
etc.

Any suggestions please?
Thank you
Johan Smit

 

Re:Bad code in handling keys?


Easy:
  if (ssCtrl in Shift) and (ssShift in Shift) and
     (Key in [VK_F1..VK_F12]) then
    begin
      Level := Key - VK_F1 + 1;
    end;

Finn Tolderlund

Johan Smit <smi...@mweb.co.za> skrev i en
nyhedsmeddelelse:383faeea.16793...@news.mweb.co.za...

Quote
> Hi,
> The following code should respond only to Control,Shift and an Fkey,
> then adjust the level variable.
> It seems awkward to me, but I do not know a better way.

> if (ssCtrl in Shift) and (ssShift in Shift) and (Key in
> VK_F1..VK_F12])
>   then begin
>    case Key of
>     VK_F1  : Level:=1;
>     VK_F2  : Level:=2;
>     VK_F3  : Level:=3;
>     VK_F4  : Level:=4;
> etc.

> Any suggestions please?
> Thank you
> Johan Smit

Re:Bad code in handling keys?


Quote
Johan Smit wrote:

> if (ssCtrl in Shift) and (ssShift in Shift) and (Key in
> VK_F1..VK_F12])
>   then begin
>    case Key of
>     VK_F1  : Level:=1;
>     VK_F2  : Level:=2;
>     VK_F3  : Level:=3;
>     VK_F4  : Level:=4;
> etc.

This will get you rid of the Case structure, but was it that
you were looking for?

begin
  if (ssCtrl in Shift) and (ssShift in Shift) and (Key in [VK_F1..VK_F12])
  then  Level := Key-111;
end;

Re:Bad code in handling keys?


In article <383faeea.16793...@news.mweb.co.za>, smi...@mweb.co.za (Johan Smit)
writes:

Quote
>if (ssCtrl in Shift) and (ssShift in Shift) and (Key in
>VK_F1..VK_F12])
>  then begin
>   case Key of
>    VK_F1  : Level:=1;
>    VK_F2  : Level:=2;
>    VK_F3  : Level:=3;
>    VK_F4  : Level:=4;
>etc.

  {A >= B means A contains B
  VK_F1 - VK_F12 are consecutive values}

Hence

  if (Shift >= [ssCtrl, ssShift) and Key in [VK_F1..VK_F12) then
    Level := Key - VK_F1 + 1; // Level is same value as F number

But if you want to protect against non-consecutive re-allocation of VK_Fn
values then :-

  if (Shift >= [ssCtrl, ssShift) then
   Case Key of
     VK_F1 : Level := 1;
     VK_F2 : Level := 2;
{etc etc}
   end;

Alan Lloyd
alangll...@aol.com
    VK_F1 :

Re:Bad code in handling keys?


Quote
In article <38405CC6.2...@iki.fi>, Markku Nevalainen <m...@iki.fi> writes:
> then  Level := Key-111;

Give Me That Old -Time Religion maybe, but don't give me them bad-code "magic
numbers" <gg>

Level := Key - VK_F1 + 1

Alan Lloyd
alangll...@aol.com

Re:Bad code in handling keys?


On Sun, 28 Nov 1999 00:35:50 +0200, Markku Nevalainen <m...@iki.fi>
wrote:
Quote
>This will get you rid of the Case structure, but was it that
>you were looking for?

>begin
>  if (ssCtrl in Shift) and (ssShift in Shift) and (Key in [VK_F1..VK_F12])
>  then  Level := Key-111;
>end;

Yes, thank you. I did not like the case statemaent there at all.
Regards
Johan Smit

Re:Bad code in handling keys?


On 28 Nov 1999 11:23:23 GMT, alangll...@aol.com (AlanGLLoyd) wrote:

Quote
>  {A >= B means A contains B
>  VK_F1 - VK_F12 are consecutive values}

>Hence

>  if (Shift >= [ssCtrl, ssShift) and Key in [VK_F1..VK_F12) then
>    Level := Key - VK_F1 + 1; // Level is same value as F numbe

Very elegant, no magic numbers.
The A contains B needed some thinking about.
No more doubts about the code.
Thank you
Regards
Johan Smit

Re:Bad code in handling keys?


Quote
AlanGLLoyd wrote:

> Give Me That Old -Time Religion maybe, but don't give me them bad-code "magic
> numbers" <gg>

> Level := Key - VK_F1 + 1

Mhh, maybe that will increase portability, if our current Delphi
codes will some day run also on Linux.

But how about, if the PC and operating system evolution brings new
function key, F0, in. Then the key mapping and numbering has to be
re-thought anyway :)

Markku Nevalainen

Re:Bad code in handling keys?


Quote
In article <38424DF2.3...@iki.fi>, Markku Nevalainen <m...@iki.fi> writes:
>But how about, if the PC and operating system evolution brings new
>function key, F0, in. Then the key mapping and numbering has to be
>re-thought anyway :)

That has nothing to do with my "magic number" comment and the possibility was
touched on in my original code suggestion.

If it did happen (even in six months time) you would be left wondering "What
the hell is 111, and why did I use that value". but I guess that if you like
laying up these sorts of hassle for yourself, that's your prerogative <gg>.

Alan Lloyd
alangll...@aol.com

Other Threads