Jump to content

[Delphi] Detecting Ctrl or Ctrl or Shift down in OnXXXX event

- - - - -

  • Please log in to reply
No replies to this topic

#1
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts
You may want that your event handler (for example OnClick event of a button) to behave differently according to whether some special key (Ctrl, Alt, or Shift) pressed or not. Here is sample code to achieve that.


procedure Form1.Button1Click(Sender: TObject);

begin

  if GetKeyState(VK_SHIFT) <> 0 then

  begin

    // run code for when Shift key is pressed when the button was clicked

    ShowMessage('Shift button is pressed');

  end

  else if GetKeyState(VK_CONTROL) <> 0 then

  begin

    // run code for when Ctrl key is pressed when the button was clicked

    ShowMessage('Ctrl button is pressed');

  end

  else if GetKeyState(VK_MENU) <> 0 then

  begin

    // run code for when Alt key is pressed when the button was clicked

    ShowMessage('Alt button is pressed');

  end;

end;


Edited by LuthfiHakim, 09 February 2011 - 09:03 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users