Results 1 to 5 of 5

Thread: How can you change the Caplock state

  1. #1
    DaoK
    Guest

    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 ?

  2. #2
    Member
    Join Date
    Feb 2001
    Posts
    57
    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.

  3. #3
    DaoK
    Guest
    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

  4. #4
    Member
    Join Date
    Feb 2001
    Posts
    57
    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 IntegerfTurnOn 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 SetNumLockBOOL bState )
    {
          
    BYTE keyState[256];

          
    GetKeyboardState((LPBYTE)&keyState);
          if( (
    bState && !(keyState[VK_NUMLOCK] & 1)) ||
              (!
    bState && (keyState[VK_NUMLOCK] & 1)) )
          {
          
    // Simulate a key press
             
    keybd_eventVK_NUMLOCK,
                          
    0x45,
                          
    KEYEVENTF_EXTENDEDKEY 0,
                          
    );

          
    // Simulate a key release
             
    keybd_eventVK_NUMLOCK,
                          
    0x45,
                          
    KEYEVENTF_EXTENDEDKEY KEYEVENTF_KEYUP,
                          
    0);
          }
    }

    void SetCapsLockBOOL bState )
    {
          
    BYTE keyState[256];

          
    GetKeyboardState((LPBYTE)&keyState);
          if( (
    bState && !(keyState[VK_CAPITAL] & 1)) ||
              (!
    bState && (keyState[VK_CAPITAL] & 1)) )
          {
          
    // Simulate a key press
             
    keybd_eventVK_CAPITAL,
                          
    0x45,
                          
    KEYEVENTF_EXTENDEDKEY 0,
                          
    );

          
    // Simulate a key release
             
    keybd_eventVK_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);

        
    SetNumLocktrue );
        
    SetCapsLocktrue );

    You can try out the code if you like. Anyone else have any ideas?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width