Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I capture modifier keys while starting a Delphi app to force some behavior

Tags:

sqlite

delphi

I'm writing an application in Delphi which uses an SQLite3 database. I'd like to be able to start the application while holding some modifier keys, such as CTRL + SHIFT, to signal reinitialization of the database.

How can I capture that the application was started while these keys were held?

like image 692
Darrel Avatar asked Dec 05 '22 07:12

Darrel


2 Answers

Tim has the right answer, but you might need a little more framework:

procedure TForm56.Button1Click(Sender: TObject);
begin
  if fNeedReinit then
    ReinitializeDatabase;
end;

procedure TForm56.FormCreate(Sender: TObject);
begin
  fNeedReinit := False;
end;

procedure TForm56.FormShow(Sender: TObject);
begin
 fNeedReinit := (GetKeyState(VK_SHIFT) < 0) and (GetKeyState(VK_CONTROL) < 0);
end;

Change Button1Click with your later event that checks to see if fNeedReinit has been set. You can also set KeyPreview on your main form if you have trouble getting it to catch the key stroke. I just tested the above code and it works, but if you have a splash screen, etc. then it might change things.

like image 191
Jim McKeeth Avatar answered Jan 01 '23 16:01

Jim McKeeth


if (GetKeyState(VK_SHIFT) < 0) and (GetKeyState(VK_CONTROL) < 0) then
  ReinitializeDatabase;
like image 28
Tim Knipe Avatar answered Jan 01 '23 15:01

Tim Knipe