Release and press key VK_SHIFT simulation

Post Reply
User avatar
FranciscoA
Posts: 1964
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Release and press key VK_SHIFT simulation

Post by FranciscoA »

Hello friends, maybe someone has already done it and can share it.

I need to simulate release and immediately press the SHIFT key while it is pressed.
That is, holding the SHIFT key, via code to make simulation that has been released and it has been pressed again immediately.

What I want to do is call a function of validation while the user holds down the SHIFT key and pressing the KEY_DOWN key on a xBrowse. This function will be called after each trigger on the KEY_DOWN key (arrow down) .

Pseudo-code:
If nKey == VK_DOWN. And. GetKeyState (VK_SHIFT)
oBrw: sendmsg (release key)
oBrw: sendmsg (keypress)
endif

Any suggestions or ideas?
Thanks in advance.

Greetings.
Saludos.
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh1204-MySql-TMySql
User avatar
fafi
Posts: 169
Joined: Mon Feb 25, 2008 2:42 am

Re: Release and press key VK_SHIFT simulation

Post by fafi »

Try this :

****************************************************************************
* READ THE KEYBOARD
****************************************************************************

***************************************************************************************************
* DESCRIPTION
*
* Read keyboard input entries and returns information about the key pressed.
* These functions intercept keyboard messages from the system will send the application
* (WM_KEYUP and WM_KEYDOWN) and stores information about the virtual key generated.
*

***************************************************************************************************
* SYNTAX:
*
* INSTALL_READ_KEYBOARD () // Install the "driver" that reads the keyboard (returns. T. if successful)
* UNINSTALL_READ_KEYBOARD () // Uninstall the "driver" that reads the keyboard (returns. T. if successful)
*
* GET_LAST_VK() // Returns the virtual value (VK Code) of the key pressed
* GET_LAST_VK_NAME() // Returns the name of the virtual key press
*
* GET_STATE_VK_SHIFT () // Returns. T. if SHIFT key is pressed
* GET_STATE_VK_CONTROL () // Returns. T. if CONTROL key is pressed
* GET_STATE_VK_ALT () // Returns. T. if ALT key is pressed
*
* PAUSE_READ_VK (.T.) // Pause reading keyboard in order to process the key pressed
* PAUSE_READ_VK (.F.) // Restore keyboard reading after the pause
*

Code: Select all

*##################################################################################################
*   C  code
*##################################################################################################

#pragma begindump

#include <windows.h>
#include "hbapi.h"

HB_BOOL flag_hhk = FALSE;
HB_BOOL PAUSE_hhk = FALSE;
HHOOK hhk = NULL;
HB_LONG VK_PRESIONADO = 0;
HB_LONG VK_lParam = 0;


LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode < 0) 
        return CallNextHookEx(hhk, nCode, wParam, lParam);
        
    if (PAUSE_hhk == FALSE)
    {   VK_PRESIONADO = (long) wParam;
        VK_lParam = (LONG) lParam;
    }
    else    
    {   VK_PRESIONADO = 0;
        VK_lParam = 0;
    }   
    
    return CallNextHookEx(hhk, nCode, wParam, lParam);
}


HB_FUNC (GET_STATE_VK_SHIFT)
{
   if (GetKeyState(VK_SHIFT) & 0x8000)
       hb_retl (TRUE); 
   else    
       hb_retl (FALSE);
}


HB_FUNC (GET_STATE_VK_CONTROL)
{
   if (GetKeyState(VK_CONTROL) & 0x8000)
       hb_retl (TRUE); 
   else    
       hb_retl (FALSE);
}


HB_FUNC (GET_STATE_VK_ALT)
{
   if (GetKeyState(VK_MENU) & 0x8000)
       hb_retl (TRUE); 
   else    
       hb_retl (FALSE);
}


HB_FUNC (GET_LAST_VK)
{
   if (flag_hhk == TRUE)
       hb_retnl (VK_PRESIONADO);
   else
      hb_retnl (0);    
}


HB_FUNC (GET_LAST_VK_NAME)
{
   CHAR cadena [128];

   if (flag_hhk == TRUE)
      {  GetKeyNameText (VK_lParam, (LPTSTR) &cadena, 128);
         hb_retc (cadena);
      }
   else
      hb_retc ("");    
}


HB_FUNC (PAUSE_READ_VK)
{
   if (hb_pcount () == 1 && hb_parinfo (1) == HB_IT_LOGICAL)   
   {   if (hb_parl (1) == TRUE) 
       {   VK_PRESIONADO = 0;
           VK_lParam = 0;
       }     
       PAUSE_hhk = hb_parl (1);
   }
}


HB_FUNC (INSTALL_READ_KEYBOARD)
{
   if (flag_hhk == FALSE)
   {    hhk = SetWindowsHookEx (WH_KEYBOARD, KeyboardProc, (HINSTANCE) NULL, GetCurrentThreadId());
        
        if (hhk == NULL) 
            hb_retl (FALSE);
        else
        {   flag_hhk = TRUE;    
            hb_retl (TRUE);                       
        }   
   }
   else
      hb_retl (TRUE);      
}


HB_FUNC (UNINSTALL_READ_KEYBOARD)
{
   if (flag_hhk == TRUE)
   {   if (UnhookWindowsHookEx (hhk) == TRUE)
       {   flag_hhk = FALSE;
           hb_retl (TRUE);           
       }
       else
           hb_retl (FALSE);   
   }
   else
      hb_retl (TRUE);      
}

#pragma enddump

 

Code: Select all


add to dialog :

oDlg:bKeyDown := { |nKey,nFlag| Pressed(nKey) }

static function Press(nKey)
   
   local cPressed :=  hb_ntos (GET_LAST_VK())
   
   
       
   if GET_STATE_VK_SHIFT() == .T. 
      
      if hb_ntos (GET_LAST_VK()) == "118" // F7
      endif   
      
      if hb_ntos (GET_LAST_VK()) == "119" // F8
      endif   

  endif
   
   if GET_STATE_VK_CONTROL() == .T. 
      
   endif
   
   if nKey == 113 //  f2
   endif   
   
   
   if nKey == 45 // insert
   endif   
   
   if nKey == 46 // delete
   endif   
   
return nil

 
Regards
Fafi
User avatar
FranciscoA
Posts: 1964
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: Release and press key VK_SHIFT simulation

Post by FranciscoA »

fafi,
thanks, I will try it.

Regards.
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh1204-MySql-TMySql
Post Reply