* * *

Author Topic: How to press and hold the ctrl key in code?  (Read 590 times)

richardcgiroux

  • New member
  • *
  • Posts: 18
How to press and hold the ctrl key in code?
« on: August 09, 2017, 03:43:15 am »
I need to press and hold down the ctrl key by checkbox state.

I need to do a multi select by mouse only so I need to simulate holding the ctrl key.

I tried:

  if checkbox1.checked then
     KeyInput.Apply([ssCTRL])
  else
    KeyInput.UnApply([ssCTRL]); 
« Last Edit: August 10, 2017, 06:48:44 pm by richardcgiroux »

engkin

  • Hero Member
  • *****
  • Posts: 1759
Re: How to grammatically press and hold the ctrl key?
« Reply #1 on: August 09, 2017, 06:07:32 am »
I need to press and hold down the ctrl key by checkbox state.

I need to do a multi select by mouse only so I need to simulate holding the ctrl key.

I tried:

  if checkbox1.checked then
     KeyInput.Apply([ssCTRL])
  else
    KeyInput.UnApply([ssCTRL]); 

Works on Windows. Don't forget to set TListBox.MultiSelect property to True, for instance.

richardcgiroux

  • New member
  • *
  • Posts: 18
Re: How to grammatically press and hold the ctrl key?
« Reply #2 on: August 09, 2017, 07:23:42 am »
I am using Linux and the app I am running needs to be on Linux. I do have the multi-select set and it works with the CTRL and Shift key pressed.  I had it working for a brief moment but can't get it to do again. I think the issue has something to do with what control has focus when the command is executed but I can't figure it out.

GetMem

  • Hero Member
  • *****
  • Posts: 2468
Re: How to grammatically press and hold the ctrl key?
« Reply #3 on: August 09, 2017, 07:48:33 am »
This works on my Kali linux distro, but first I had to install libxtst-dev:
Code: Pascal  [Select]
  1. uses MouseAndKeyInput;
  2.  
  3.  
  4. procedure TForm1.ListBox1MouseEnter(Sender: TObject);
  5. begin
  6.   if CheckBox1.Checked then
  7.     KeyInput.Apply([ssCTRL]);
  8. end;
  9.  
  10. procedure TForm1.ListBox1MouseLeave(Sender: TObject);
  11. begin
  12.   if CheckBox1.Checked then
  13.     KeyInput.UnApply([ssCTRL]);
  14. end;
  15.  
  16. procedure TForm1.OnDeactivate(Sender: TObject);
  17. begin
  18.   KeyInput.UnApply([ssCTRL]);
  19. end;
  20.  
  21. procedure TForm1.FormCreate(Sender: TObject);
  22. begin
  23.   Application.OnDeactivate := @OnDeactivate;
  24. end;

PS: In my opinion leaving the CTRL flag on(system-wide), it's not a good idea.
« Last Edit: August 09, 2017, 07:56:24 am by GetMem »

richardcgiroux

  • New member
  • *
  • Posts: 18
Re: How to press and hold the ctrl key in code?
« Reply #4 on: August 10, 2017, 06:51:17 pm »
Thanks.

The code that I was using didn't work on my dev machine but did work on the final platform.  Thanks.

I take your warning about setting the global CTRL key but in this case, my program will be the only app running on it and it is a kiosk, keyboard-less installation so it is necessary.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus