<BG> No I wasn't kidding, but now I see where I was being foolish
and not thinking! <g> I simply wasn't considering "Key" to be a variable
which was pretty dumb even for me! Also, when I was looking at it in
decimal notation only, that's why I was confused by the hexadecimal
notation. That's something I really need to study. I also wasn't aware
that Delphi doesn't support binary! Is that true only for numerics?
Now you said that GetKeyState could return any bit...meaning what in
actual practice...that I shouldn't rely on the return as given and that I
should mask it as well? The help says it returns the high order bit so
doesn't that mean the function is doing that for me?
from Robert Meek dba Tangentals Design CCopyright 2006
"When I examine myself and my methods of thought, I come to the conclusion
that the gift of Fantasy has meant more to me then my talent for absorbing
positive knowledge!"
Albert Einstein
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Rob Kennedy
Sent: Thursday, April 27, 2006 2:40 AM
To: Borland's Delphi Discussion List
Subject: Re: Virtual key codes
Robert Meek wrote:
> Actually I learned that a long time ago and keep an updated copy on
> my desktop. But having read the documentation I still didn't
> understand why it was necessary to mask out the other bits in one
> situation and not another.
Who said it wasn't necessary in the other?
> GetKeyState(VK_SHIFT) works fine by itself so evidently it is
> returning the value of the high order bit.
It could be returning a value in *any* bit. Asking whether a number is
greater than zero is not the same as asking whether it can be expressed
as the sum of 32768 plus other distinct powers of two.
> Also I don't have a clue as to what $8000 means or how it works...I'm
> just guessing...remember no comp sci at all!
As I mentioned before, GetKeyState returns a Word -- that's a 16-bit
type. The help says the high-order bit is the one we should look at. The
"high-order bit" is the most significant bit. Written in binary, our
mask would be 1000000000000000. That's a one in the highest bit and
zeros for the other 15. Delphi doesn't support binary notation for
numeric literals, so we bit the next best thing, hexadecimal, in which
case the mask is 8000. We could also have expressed it in decimal as 32768.
When we use the "and" operator, it compares its operands bit-by-bit.
When a bit is set -- equal to one -- in both operands, then that bit
will also be set in the result. If either of the operands' bits is zero,
then the corresponding bit in the result will be zero.
http://en.wikipedia.org/wiki/Binary_and
Since our mask only has one bit set, we're guaranteed that everything
*other* than that one bit will be cleared -- equal to zero -- in the
result, allowing us to focus on just the bit we're interested in -- the
bit that MSDN told us we should be interested in. That one bit is now
solely responsible for determining whether the result is zero. If the
bit is cleared -- if the key is not down -- then the result is zero; if
the bit is set, then the result is nonzero and the key is down.
> Now Malcolm is saying that simply if (key=VK_F1) and (ssShift in
> Shift) then should work as well,
Correct. The Shift parameter to the OnKeyDown event handler tells the
states of the Shift, Alt, and Ctrl keys.
> but why then doesn't if (key=VK_F1) and (Key = VK_SHIFT) then work as
> well?
You're kidding, right?
For the same reason "if (x = 1) and (x = 2)" won't work.
> They both check for any Shift Key?
One checks for Shift keys. The other checks for nonsense.
> Is it a syntax problem that you cannot check for two virtual key
> codes together?
No, the compiler is perfectly happy to compile that code. It will never
evaluate to True, though, because no variable can have two values
simultaneously. A sufficiently smart compiler (or other static-analysis
tool) might even warn about that.
--
Rob
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi