Toon posts:

[Delphi] statusbalk en numlock

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik ben voor school bezig om de interface van Access na te bouwen. Het gaat hier verder niet om het programmeren van alle functies. Maar nou was ik bezig met de statusbalk onderin. En bij Access gaat Numlock branden wanneer ik deze op het toetsenbord aanzet en vise versa.

Nou werd ik gewoon eens nieuwsgierig en ik wilde eens weten of je dat in Delphi ook kon programmeren. Heb al gezocht op het forum en op google. Maar ik kan het niet echt ergens vinden, ben niet zo'n programmeer held in Delphi maar het leek me wel leuk als het werkte. Mijn vraag:
Heeft iemand een relatief simpele manier om dit tot stand te brengen?

Verwijderd

Zijn genoeg componenten die dit kunnen, heb je al op torry gezocht ?
www.torry.net

of zelf maken, hiermee kan je de status van die toetsen uitvragen, nu moet je nog ff een manier vinden om te kijken of ie veranderd terwijl je programma draai
(hint: keyboardproc, setwindowshookex) :
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
The GetKeyState function retrieves the status of the specified virtual key. The 
status specifies whether the key is up, down, or toggled (on, off ¾ 
alternating each time the key is pressed). 
SHORT GetKeyState(


   int nVirtKey     // virtual-key code
   );

Parameters

nVirtKey

Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a 
trough z, or 0 through 9), nVirtKey must be set to the ASCII value of that 
character. For other keys, it must be a virtual-key code. 

If a non-English keyboard layout is used, virtual keys with values in the range 
ASCII A through Z and 0 through 9 are used to specify most of the character keys. 
For example, for the German keyboard layout, the virtual key of value ASCII O 
(0x4F) refers to the "o" key, whereas VK_OEM_1 refers to the "o with umlaut" key. 

Return Values

If the function succeeds, the return value specifies the status of the given virtual 
key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order 
bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is 
turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's 
indicator light (if any) on the keyboard will be on when the key is toggled, and off 
when the key is untoggled. 

Remarks

The key status returned from this function changes as a given thread reads key 
messages from its message queue. The status does not reflect the interrupt-level 
state associated with the hardware. Use the GetAsyncKeyState function to retrieve 
that information. 

An application calls GetKeyState in response to a keyboard-input message. This 
function retrieves the state of the key when the input message was generated. 

To retrieve state information for all the virtual keys, use the GetKeyboardState 
function. 

An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and 
VK_MENU as values for the nVirtKey parameter. This gives the status of the SHIFT, 
CTRL, or ALT keys without distinguishing between left and right. An application can 
also use the following virtual-key code constants as values for nVirtKey to 
distinguish between the left and right instances of those keys: 

VK_LSHIFT   VK_RSHIFT
VK_LCONTROL VK_RCONTROL
VK_LMENU    VK_RMENU

These left- and right-distinguishing constants are available to an application only 
through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, 
GetKeyState, and MapVirtualKey functions. 

See Also
GetAsyncKeyState, GetKeyboardState, MapVirtualKey, SetKeyboardState

[ Voor 97% gewijzigd door Verwijderd op 16-11-2003 21:15 ]


Verwijderd

Of probeer het zo:
Link naar Google Groups

Verwijderd

Topicstarter
Verwijderd schreef op 16 november 2003 @ 21:05:
Zijn genoeg componenten die dit kunnen, heb je al op torry gezocht ?
www.torry.net

of zelf maken, hiermee kan je de status van die toetsen uitvragen, nu moet je nog ff een manier vinden om te kijken of ie veranderd terwijl je programma draai
(hint: keyboardproc, setwindowshookex) :
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
The GetKeyState function retrieves the status of the specified virtual key. The 
status specifies whether the key is up, down, or toggled (on, off ¾ 
alternating each time the key is pressed). 
SHORT GetKeyState(


   int nVirtKey     // virtual-key code
   );

Parameters

nVirtKey

Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a 
trough z, or 0 through 9), nVirtKey must be set to the ASCII value of that 
character. For other keys, it must be a virtual-key code. 

If a non-English keyboard layout is used, virtual keys with values in the range 
ASCII A through Z and 0 through 9 are used to specify most of the character keys. 
For example, for the German keyboard layout, the virtual key of value ASCII O 
(0x4F) refers to the "o" key, whereas VK_OEM_1 refers to the "o with umlaut" key. 

Return Values

If the function succeeds, the return value specifies the status of the given virtual 
key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order 
bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is 
turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's 
indicator light (if any) on the keyboard will be on when the key is toggled, and off 
when the key is untoggled. 

Remarks

The key status returned from this function changes as a given thread reads key 
messages from its message queue. The status does not reflect the interrupt-level 
state associated with the hardware. Use the GetAsyncKeyState function to retrieve 
that information. 

An application calls GetKeyState in response to a keyboard-input message. This 
function retrieves the state of the key when the input message was generated. 

To retrieve state information for all the virtual keys, use the GetKeyboardState 
function. 

An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and 
VK_MENU as values for the nVirtKey parameter. This gives the status of the SHIFT, 
CTRL, or ALT keys without distinguishing between left and right. An application can 
also use the following virtual-key code constants as values for nVirtKey to 
distinguish between the left and right instances of those keys: 

VK_LSHIFT   VK_RSHIFT
VK_LCONTROL VK_RCONTROL
VK_LMENU    VK_RMENU

These left- and right-distinguishing constants are available to an application only 
through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, 
GetKeyState, and MapVirtualKey functions. 

See Also
GetAsyncKeyState, GetKeyboardState, MapVirtualKey, SetKeyboardState
haha heel aardig zo'n lap tekst..maar zoals gezegd ik ben niet echt een programmeur. Ik dacht als iemand het toevallig een keer gedaan had dat ie dan ff wat uitleg kon geven of een code..heb idd wel gevonden dat hij de status aangeeft op het moment dat ik het proggie draai maar idd veranderd ie de status ervan nie.

Verwijderd

Uitleg van keyboardproc, setwindowshookex & unhookwindowsex staat in de delphi help

Delphi:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var
  MyHook : hhook;
  Form1: TForm1;

implementation

uses JclSysInfo;


{$R *.DFM}


procedure MyKeyboardProc(code : integer;    // hook code
                         WPARAM :wParam;    // virtual-key code
                         LPARAM :lParam     // keystroke-message information
                          );
begin
 CallNextHookEx(myhook, code, wparam, lparam);
  if GetCapsLockKeyState() then form1.edit1.text := 'on'
  else form1.edit1.text := 'off';
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnhookWindowsHookEx(myhook);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  if GetCapsLockKeyState() then form1.edit1.text := 'on'
  else form1.edit1.text := 'off';
  myhook := setwindowshookex(WH_KEYBOARD, @mykeyboardproc, HINSTANCE,  0);
end;

end.

[ Voor 14% gewijzigd door Verwijderd op 16-11-2003 22:02 ]


  • Just_a_Gamer
  • Registratie: November 2001
  • Laatst online: 13:43
euhmm dit werkt ook :)

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, AppEvnts;

type
  TForm1 = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    StatusBar1: TStatusBar;
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  ShellApi;

procedure TForm1.ApplicationEvents1Idle(Sender: TObject;
  var Done: Boolean);
begin
  if GetKeyState( VK_NUMLOCK ) = 1 then
    StatusBar1.Panels[ 0 ].Text := 'NUMLOCK ON'
  else
    StatusBar1.Panels[ 0 ].Text := 'NUMLOCK OFF';
end;

end.


ervan uitgaande dat op je form een TApplicationEvent heb :) Die van mij controleert hele tijd of NUMLock aan staat wanneer het programma idle'd

Verwijderd

Just_a_Gamer schreef op 16 november 2003 @ 22:06:
euhmm dit werkt ook :)

lap code

ervan uitgaande dat op je form een TApplicationEvent heb :) Die van mij controleert hele tijd of NUMLock aan staat wanneer het programma idle'd
Dit is niet echt de aan te raden manier omdat dit, zoals je zelf al zegt, alleen bij idle z'n werk doet. Beter kun je dit in een aparte thread doen of in de TActionList-update.
Gebruik van TTimers is ook niet aan te raden omdat de maximum aantal timers OS/systeem-gebonden zijn en het dus kan voor komen dat een andere applicatie alle beschikbare timers al heeft opgebruikt.

  • LordLarry
  • Registratie: Juli 2001
  • Niet online

LordLarry

Aut disce aut discede

Je raad nooit wanneer de TActionList.Update aangeroepen wordt :D In de Application.OnIdle! Die OnIdle wordt heel regelmatig aangeroepen hoor. :)

We adore chaos because we like to restore order - M.C. Escher


Verwijderd

LordLarry schreef op 17 november 2003 @ 11:32:
Je raad nooit wanneer de TActionList.Update aangeroepen wordt :D In de Application.OnIdle! Die OnIdle wordt heel regelmatig aangeroepen hoor. :)
Sjit, je hebt gelijk, ik zit net in de help te loeren en daar staat 't inderdaad. Maar goed. dan lijkt het me het verstandigst om een aparte thread te gebruiken. Als nl. je applicatie niet idled (bijv. bij een update actie over een grote tabel) dan werkt het principe niet meer. En het is vaak zo dat gebruikers denken dat dan juist de applicatie vast zit. Als je GUI dan nog wel reactie geeft (bijv. met de capslock status) dan ben je van dit probleem af, of je gebruikt een progressbar.
offtopic:
Bovendien bouw ik sowieso altijd mijn event-handlers in aparte Observer/Listener-threads aangezien ik het VCL event-handling concept niet de mooiste oplossing vind.
Pagina: 1