Explosion with minimal resource, display system key status

xiaoxiao2021-03-06  93

Many people discuss the problem of displaying system keys (Capslock, Numlock, INSERT, SCROLLLLLLLLLLLLLLLOCK, etc.), using the most ways to add a Timer, refresh the system key status in the event; another method is to do a system hook , Refresh the display in the hook. Both methods will seriously occupy system resources, and there is still a problem with the first method. Now introducing the third method: The keyboard button is pressed when the system will send WM_KEYDOWN, WM_CHAR, WM_KEYUP message to the current activation application, the message is wparam is the keyboard scanning code, so we can know if the button is pressed or released, In this refresh button status is the best time. However, there is still a problem, the application is not allowed to receive the above message when the application is inactive, so it needs to be detected and refreshed when the program is activated.

The code is given below: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls; type TForm1 = class (TForm) StatusBar1: TStatusBar; procedure FormCreate (Sender: TObject); private procedure AppActivate (Sender: TObject); procedure AppOnMsg (var Msg: tagMSG; var Handled: Boolean); procedure ShowKeyState; // key state display system end; var Form1: TForm1; implementation {$ R * .dfm} {== ============================================================================================================================================================================================================= =============== Design By: Peng Guofei Date: http://kacarton.yeah.net/ blog: http://blog.9cbs.net/ NHCONCH EMAIL: Kacarton@sohu.com Article is the author original, please contact him before reprinting, please indicate the article, retain the author information, thank you for your support! ============================================================================================================================================================================================================= ================================================================================================ • .OnActivate: = AppActivate; end; procedure TForm1.AppActivate (Sender: TObject); begin ShowKeyState; refresh button status system is activated // end application; procedure TForm1.ShowKeyState; begin if (GetKeyState (145) and 1) < >

0 dams [6] .text: = 'scor' else statusbar1.panels.items [6] .text: = '; if (getKeyState (144) and 1) <> 0 Then Statusbar1.Panels. Items [5] .Text: = 'Num' else statusbar1.panels.items [5] .text: = '; if (getKeyState (45) and 1) <> 0 Then Statusbar1.Panels.Items [4] .text : = 'Insert' Else Statusbar1.Panels.Items [4] .text: = 'override'; if (getKeyState (20) and 1) <> 0 Then Statusbar1.Panels.Items [3] .text: = 'uppercase' Else statusbar1.panels.items [3] .text: = 'lowercase'; end; procedure tform1.apponmsg (var Msg: tagmsg; var handled: boolean); begin if msg.MESSAGE = 256 THEN BEGIN / / WM_KEYDOWN CASE MSG. WPARAM OF / / Display 145: // Scorll IF according to the button (GetKeyState (145) and 1) <> 0 Then Statusbar1.Panels.Items [6] .text: = 'scor' else statusbar1.panels.Items [6] .Text: = ''; 144: // Num IF (getKeyState (144) and 1) <> 0 THEN Statusbar1.panels.Items [5] .text: = 'Num' else statusbar1.panels.items [5] .text: = '; 45: // insure (getKeyState (45) and 1) <> 0 THEN STATUSBAR1 .Panels.items [4] .Text: = 'Insert' else statusbar1.panels.items [4] .text: = 'overlay'; 20: // Caps IF (getKeyState (20) and 1) <> 0 Then Statusbar1 .Panels.items [3] .Text: = 'uppercase'

转载请注明原文地址:https://www.9cbs.com/read-122136.html

New Post(0)