Mega Code Archive

 
Categories / Delphi / API
 

Detecting if the printscreen key has been pressed

Question: How can I tell if the PrintScreen key has been pressed? Answer: The PrintScreen system key is not processed during the TForm keydown event. The following example tests if the PrintScreen key has been pressed by calling the Windows API function GetAsyncKeyState() during the Application.OnIdle event. Example: type TForm1 = class(TForm) Button1: TButton; procedure FormCreate(Sender: TObject); private { Private declarations } procedure AppIdle(Sender: TObject; var Done: Boolean); public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin Application.OnIdle := AppIdle; end; procedure TForm1.AppIdle(Sender: TObject; var Done: Boolean); begin if GetAsyncKeyState(VK_SNAPSHOT) <> 0 then Form1.Caption := 'SnapShot'; Done := True; end;