How can you change the caplock and numlock state ?
I tryed something like that :
But it doesnt work ??? Why ?Code:asm{
mov ax, 0x0040
mov es, ax
mov ah, es:[0x0017]
xor ah, 0x40
mov es:[0x0017], ah
}
Printable View
How can you change the caplock and numlock state ?
I tryed something like that :
But it doesnt work ??? Why ?Code:asm{
mov ax, 0x0040
mov es, ax
mov ah, es:[0x0017]
xor ah, 0x40
mov es:[0x0017], ah
}
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.
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
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:
And here's the C++ versions (2):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
You can try out the code if you like. Anyone else have any ideas?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 );
}
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).Code:GetKeyboardState((LPBYTE)&keyState);
And the same for the other function :)