Page 1 of 1

KeyToggle() and Num, Caps and Ins keys

Posted: Thu Feb 09, 2012 3:25 pm
by frose
Hi,

want to switch in insert mode with
- KeyToggle( VK_INSERT ) or
- Eval( oWnd:oMsgBar:oKeyIns:bAction ) or
- Mouse click in the message bar on 'Ins'
but it's not working :(

In addition the NUMLOCK- and CAPSLOCK-LED's on the keyboard don't react, if the Buttons 'Num' or 'Caps' are mouse clicked in the message bar :(

With the informations out of these two threads:
http://forums.fivetechsupport.com/viewt ... f=6&t=2551
http://forums.fivetechsupport.com/viewt ... f=3&t=1577

I changed the method 'KeybOn()' in msgbar.prg as follows:
- Replaced 'KeyToggle( VK_NUMLOCK )' with 'SetNumLock( !GetNumLock() )' in ::oKeyNum
- Replaced 'KeyToggle( VK_CAPITAL )' with 'SetCapsLock( !GetCapsLock() )' in ::oKeyCaps
- Added ', Set( _SET_INSERT, ! Set( _SET_INSERT ) )' in ::oKeyIns

Here is the complete method and the C functions:

Code: Select all

METHOD KeybOn() CLASS TMsgBar

   if ::oKeyNum == nil
      // keep the ':=' below for XBase++ compatibility
      ::oKeyNum := TMsgItem():New( Self, "Num", ::GetWidth( "Num" ) + 12,,,, .t.,;
                                  { || SetNumLock( !GetNumLock() ), ::oKeyNum:Refresh() } )
      ::oKeyNum:lTimer = .t.
      ::oKeyNum:nClrDisabled = GetSysColor( COLOR_BTNSHADOW )
      ::oKeyNum:lActive = GetKeyToggle( VK_NUMLOCK )
      ::oKeyNum:bMsg  = { || ::oKeyNum:lActive := GetKeyToggle( VK_NUMLOCK ), "Num" }
   endif

   if ::oKeyCaps == nil
      // keep the ':=' below for XBase++ compatibility
      ::oKeyCaps := TMsgItem():New( Self, "Caps", ::GetWidth( "Caps" ) + 12,,,, .t.,;
                                  { || SetCapsLock( !GetCapsLock() ), ::oKeyCaps:Refresh() } )
      ::oKeyCaps:lTimer = .t.
      ::oKeyCaps:nClrDisabled = GetSysColor( COLOR_BTNSHADOW )
      ::oKeyCaps:lActive = GetKeyToggle( VK_CAPITAL )
      ::oKeyCaps:bMsg = { || ::oKeyCaps:lActive := GetKeyToggle( VK_CAPITAL ), "Caps" }
   endif

   if ::oKeyIns == nil
      // keep the ':=' below for XBase++ compatibility
      ::oKeyIns := TMsgItem():New( Self, "Ins", ::GetWidth( "Ins" ) + 12,,,, .t.,;
                                  { || KeyToggle( VK_INSERT ), Set( _SET_INSERT, ! Set( _SET_INSERT ) ), ::oKeyIns:Refresh() } )
      ::oKeyIns:lTimer = .t.
      ::oKeyIns:nClrDisabled = GetSysColor( COLOR_BTNSHADOW )
      ::oKeyIns:lActive = GetKeyToggle( VK_INSERT )
      ::oKeyIns:bMsg = { || ::oKeyIns:lActive := GetKeyToggle( VK_INSERT ), "Ins" }
   endif

   ::CheckTimer()

return nil
#pragma BEGINDUMP

#include <windows.h>

#include "hbapi.h"

#define TOGGLED 0x0001

void SetKeyLock( BYTE vKey, BOOL bState )
{
   BOOL bCurrentState;

   bCurrentState = ( GetKeyState( vKey ) & TOGGLED );

   if( (bState && !bCurrentState) ||
       (!bState && bCurrentState) )
   {
      // Simulate a key press
      keybd_event( vKey,
                   0,
                   KEYEVENTF_EXTENDEDKEY | 0,
                   0 );

      // Simulate a key release
      keybd_event( vKey,
                   0,
                   KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                   0);
   }
}

BOOL GetKeyLock( int vKey )
{
   return ( GetKeyState( vKey ) & TOGGLED );
}

HB_FUNC( SETCAPSLOCK )
{
   SetKeyLock( VK_CAPITAL, hb_parl( 1 ) );
}

HB_FUNC( SETNUMLOCK )
{
   SetKeyLock( VK_NUMLOCK, hb_parl( 1 ) );
}

HB_FUNC( SETINSLOCK )
{
   SetKeyLock( VK_INSERT, hb_parl( 1 ) );
}
HB_FUNC( GETCAPSLOCK )
{
   hb_retl( GetKeyLock( VK_CAPITAL ) );
}

#pragma ENDDUMP
Now I can set the insert mode by mouse click or with:

Code: Select all

...
KeyToggle( VK_INSERT )
Set( _SET_INSERT, .T. )
...
 
And the NUMLOCK- and CAPSLOCK-LED's on the keyboard are consistent with the FiveWin settings :D

Re: KeyToggle() and Num, Caps and Ins keys

Posted: Tue Mar 02, 2021 12:10 pm
by karinha
Very good!

Regards.