How can I grab modifier keys when running a Delphi application to force some behavior

I am writing an application in Delphi that uses a SQLite3 database. I would like to be able to launch the application by holding down some modifier keys, such as CTRL + SHIFT, to signal that the database is reinitializing.

How can I record that the application was running while these keys were saved?

+4
source share
4 answers

Tim has the correct answer, but you may need a little more scope:

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 Button1 Click your later event, which checks if fNeedReinit is installed. You can also install KeyPreview in your main form if you have problems catching it. I just tested the above code and it works, but if you have a splash screen, etc., this can make a difference.

+8
source
 if (GetKeyState(VK_SHIFT) < 0) and (GetKeyState(VK_CONTROL) < 0) then ReinitializeDatabase; 
+7
source

Try using GetAsyncKeyState , GetKeyState or GetKeyboardState API Functions to read the current state of the ctrl and shift keys when the program starts. Adding a keyboard hook at startup may not work, because keypress events for the switch keys might occur before your application can set the hook.

+1
source

You have to grab the keyboard hooks in your application. See here and then process the hooks before you show the main form - for example, before CreateForm and run in the dpr file

0
source

Source: https://habr.com/ru/post/1277778/


All Articles