Recent

Author Topic: How do I read 'Control Key + A' in onkeydown event??  (Read 7797 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
How do I read 'Control Key + A' in onkeydown event??
« on: December 06, 2010, 05:06:23 pm »
Ok, after digging a bit I'm either asking the wrong question, using the wrong phrases OR no one has ever asked this before, guess which one I think it is  :-[

I'm using an onkeydown event to trap the key presses of a form and have no problems with the normal keys, including the Control key but I'm not sure how I determine when I have key combinations pressed.  I need to use Cntrl A and Cntrl D in an editor scenario and I can see when the cntrl key is pressed but how do I trap BOTH keys so I know the user is pressing them at the same time??

 

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How do I read 'Control Key + A' in onkeydown event??
« Reply #1 on: December 06, 2010, 05:16:08 pm »
KeyPreviwew On:

Code: [Select]
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
  // uses LCLType
  if (ssCtrl in Shift)and (Key = VK_A)
    then ShowMessage('Ctrl + a');
end;
« Last Edit: December 06, 2010, 05:23:32 pm by typo »

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: How do I read 'Control Key + A' in onkeydown event??
« Reply #2 on: December 06, 2010, 05:16:51 pm »
As is often the case when I went back and tried yet another key word combination search I found the answer.  So for anyone looking for something similar here is what I came up with.

To trap Control+A in the onkeydown event use:

if(key=ord('A')) and (ssCtrl in Shift) then

I'm guessing you could sub in Alt if needed but I didn't try that since I only needed the Control key.

I had seen something like this on an earlier search but it wasn't as clear as what I finally found and posted above.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How do I read 'Control Key + A' in onkeydown event??
« Reply #3 on: December 06, 2010, 05:18:12 pm »
Quote
uses Windows
Better: LCLType (cross platform)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: How do I read 'Control Key + A' in onkeydown event??
« Reply #4 on: December 06, 2010, 05:20:44 pm »
KeyPreviwew On:

Code: [Select]
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
  // uses Windows
  if (ssCtrl in Shift)and (Key = VK_A)
    then ShowMessage('Ctrl + a');
end;

Thanks, I found a similar solution on the net for Delphi coding which worked.  Did you add the '// uses windows' because you are on a windows box or is this something that shouldn't work on a linux box??  I ask this since the box I'm working on is Linux and the above code I posted works but the eventual target machines will be windows so I want to make sure what I'm using will cross platforms.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How do I read 'Control Key + A' in onkeydown event??
« Reply #5 on: December 06, 2010, 05:24:05 pm »
I have edited the post.

 

TinyPortal © 2005-2018