|
-
May 7th, 2002, 07:36 PM
#1
How can you change the Caplock state
How can you change the caplock and numlock state ?
I tryed something like that :
Code:
asm{
mov ax, 0x0040
mov es, ax
mov ah, es:[0x0017]
xor ah, 0x40
mov es:[0x0017], ah
}
But it doesnt work ??? Why ?
-
May 8th, 2002, 07:45 PM
#2
Member
Simple. Use GetKeyboardState and SetKeyboardState from the Win32 API to manipulate these two keys. Pass an array with your character states to these two functions.
-
May 9th, 2002, 02:12 PM
#3
I never used api in C++... maybe you can give me a code that will introduce me on it please.
I will really appreaciate 
Daok
-
May 10th, 2002, 04:38 PM
#4
Member
Sorry, but I seem to be having some trouble with creating code that actually works correctly. I have code that works correctly in VB but not in VC++. Here's the VB code that works:
PHP Code:
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
Private Sub SetKeyState(intKey As Integer, fTurnOn As Boolean)
' Retrieve the keyboard state, set the particular
' key in which you're interested, and then set
' the entire keyboard state back the way it
' was, with the one key altered.
Dim abytBuffer(0 To 255) As Byte
GetKeyboardState abytBuffer(0)
abytBuffer(intKey) = CByte(Abs(fTurnOn))
SetKeyboardState abytBuffer(0)
End Sub
And here's the C++ versions (2):
PHP Code:
#include <windows.h>
void SetNumLock( BOOL bState )
{
BYTE keyState[256];
GetKeyboardState((LPBYTE)&keyState);
if( (bState && !(keyState[VK_NUMLOCK] & 1)) ||
(!bState && (keyState[VK_NUMLOCK] & 1)) )
{
// Simulate a key press
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0 );
// Simulate a key release
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
}
}
void SetCapsLock( BOOL bState )
{
BYTE keyState[256];
GetKeyboardState((LPBYTE)&keyState);
if( (bState && !(keyState[VK_CAPITAL] & 1)) ||
(!bState && (keyState[VK_CAPITAL] & 1)) )
{
// Simulate a key press
keybd_event( VK_CAPITAL,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0 );
// Simulate a key release
keybd_event( VK_CAPITAL,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
}
}
void main()
{
unsigned char KeyBoardArray[256];
GetKeyboardState(KeyBoardArray);
KeyBoardArray[20/*Caps Lock*/] = (char)1; // Or 0 for off
KeyBoardArray[144/*Num Lock*/] = (char)1; // Or 0 for off
KeyBoardArray[145/*Scroll Lock*/] = (char)1; // Or 0 for off
SetKeyboardState(KeyBoardArray);
SetNumLock( true );
SetCapsLock( true );
}
You can try out the code if you like. Anyone else have any ideas?
-
May 11th, 2002, 11:21 AM
#5
Monday Morning Lunatic
Code:
GetKeyboardState((LPBYTE)&keyState);
This bit should just be GetKeyboardState(keyState) since it's already a pointer (not indexing into the array returns a pointer to the first element).
And the same for the other function
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|