tags:

views:

393

answers:

4

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?

A: 

You have to capture keyboard hooks in your application. See Here and then process the hooks before you show the main form - eg before the CreateForm and Run in the dpr file

KiwiBastard
+1  A: 

Try one of GetAsyncKeyState, GetKeyState or GetKeyboardState API functions to read the current state of the ctrl and shift keys at program startup. Adding a keyboard hook at startup may not work since the key press events for the shift keys could have occurred before your application has a chance to install the hook.

Ates Goral
+7  A: 
if (GetKeyState(VK_SHIFT) < 0) and (GetKeyState(VK_CONTROL) < 0) then
  ReinitializeDatabase;
Tim Knipe
+6  A: 

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.

Jim McKeeth