Re: Detecting shift/alt keys



On Thu, 21 Apr 2005 15:40:02 +0100, Peter <peter@xxxxxxxxxxx> waffled
on about something:

>On Thu, 21 Apr 2005 14:24:54 +0100, Dodgy
><Dodgy@xxxxxxxxxxxxxxxxxxxxx> wrote:
>
>>On Thu, 21 Apr 2005 13:40:04 +0100, Peter <peter@xxxxxxxxxxx> waffled
>>on about something:
>>
>>>Hi,
>>>
>>>I have a form with several controls.
>>>
>>>How do I detect when a user presses the shift or alt keys? I can't
>>>seem to trap any keys using formkeypress or formkeydown.
>>>
>>>I have a couple of speed buttons and I want them to also be activated
>>>on either a SHIFT or ALT key being pressed.
>>>
>>>Thanks.
>>
>> if HiWord(GetKeyState(vk_shift))<>0 then
>> // it's down
>> else
>> // it's up
>>
>>The alt key is vk_menu
>>
>>Full list here.
>>
>>http://delphi.about.com/od/objectpascalide/l/blvkc.htm
>>
>>Hope this helps.
>
>Thanks.
>
>What I don't understand is where I should be checking. Form.onkeydown
>and form.onkeypress don't seem to do anything.
>
>Sorry but I'm a bit of a newbie to Delphi!

The onkeydown, onkeyup aren't issued for the "special" keys like
shift, ctrl and alt.

Generally the "special" keys are used in combination with a "normal"
key for special effects, so you pick up on the "P" being pressed in
the onkeypress event, and then just check if any of the special keys
are down at the time, and act accordingly.

Unfortunately you're using the detection of these keys to make a menu
appear or change without any other keys being pressed, you either need
to find a windows event for any keypress to hook into and check, or
check using a timer or actionlist.

I'm sure at this point someone will chip in an give an example of
detecting a keypress from a windows message, which you could trap and
use to check if it's a key you're interested in, which is the most
elegant way of doing things.

The quick and dirty is to chuck a timer on your form, set the interval
to 200 (.2second) and in the timer event put in the code to check for
the keys. Don't forget to enable the timer! :-)

Dodgy.
--
MUSHROOMS ARE THE OPIATE OF THE MOOSES
.