READ THE KEYBOARD

You can share your experience with HMG. Share with some screenshots/project details so that others will also be benefited.

Moderator: Rathinagiri

User avatar
srvet_claudio
Posts: 1919
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 25 times
Been thanked: 96 times
Contact:

READ THE KEYBOARD

Post by srvet_claudio » Wed Apr 14, 2010 2:14 am

Hola a todos,
como inquietud de uno de los participantes del foro les dejo aquí algunas funciones básicas para procesar las entradas del teclado.
Espero que les sea de alguna utilidad.
Un saludo cordial a todos,
Claudio Soto


***************************************************************************************************
* DESCRIPCION
*
* Lee las entradas entradas del teclado y devuelve informacion sobre la tecla presionada.
* Estas funciones interceptan los mensajes de teclado que el sistema le envia a la aplicación
* (WM_KEYUP y WM_KEYDOWN) y almacena informacion sobre la tecla virtual generada.
*
***************************************************************************************************

***************************************************************************************************
* SINTAXIS:
*
* INSTALL_READ_KEYBOARD () // Instala el manejador que lee el teclado (Retorna .T. si tiene exito)
* UNINSTALL_READ_KEYBOARD () // Desinstala el manejador que lee el teclado (Retorna .T. si tiene exito)
*
* GET_LAST_VK() // Retorna el valor virtual (VK Code) de la tecla presionada
* GET_LAST_VK_NAME() // Retorna el nombre de la tecla virtual presionado
*
* GET_STATE_VK_SHIFT () // Retorna .T. si la tecla SHIFT esta presionada
* GET_STATE_VK_CONTROL () // Retorna .T. si la tecla CONTROL esta presionada
* GET_STATE_VK_ALT () // Retorna .T. si la tecla ALT esta presionada
*
* PAUSE_READ_VK (.T.) // Pausa la lectura del teclado para poder procesar la tecla presionada
* PAUSE_READ_VK (.F.) // Restablece la lectura del teclado luego de la pausa
*
*******************************************************************************************************
* Los valores virtuales (VK Code) de las diferentes teclas pueden encontrarlos en el archivo
* C:\hmg.3.0.29\MINGW\include\winuser.h
*
* Comienzan con el prefijo #define VK_
* Por ejemplo, #define VK_SHIFT 16
*******************************************************************************************************


Code: Select all

****************************************************************************
* PROGRAMA: READ THE KEYBOARD 
* LENGUAJE: HARBOUR-MINIGUI 3.0.29
* FECHA:    13 ABRIL 2010
* AUTOR:    CLAUDIO SOTO
* PAIS:     URUGUAY
* E-MAIL:   srvet@adinet.com.uy
****************************************************************************

***************************************************************************************************
* DESCRIPCION
*
* Lee las entradas entradas del teclado y devuelve informacion sobre la tecla presionada. 
* Estas funciones interceptan los mensajes de teclado que el sistema le envia a la aplicación 
* (WM_KEYUP y WM_KEYDOWN) y almacena informacion sobre la tecla virtual generada.  
*
***************************************************************************************************

***************************************************************************************************
* SINTAXIS:
*
* INSTALL_READ_KEYBOARD ()   // Instala el manejador que lee el teclado (Retorna .T. si tiene exito) 
* UNINSTALL_READ_KEYBOARD () // Desinstala el manejador que lee el teclado (Retorna .T. si tiene exito)
*
* GET_LAST_VK()              // Retorna el valor virtual (VK Code) de la tecla presionada 
* GET_LAST_VK_NAME()         // Retorna el nombre de la tecla virtual presionado
*
* GET_STATE_VK_SHIFT ()      // Retorna .T. si la tecla SHIFT esta presionada
* GET_STATE_VK_CONTROL ()    // Retorna .T. si la tecla CONTROL esta presionada
* GET_STATE_VK_ALT ()        // Retorna .T. si la tecla ALT esta presionada
*
* PAUSE_READ_VK (.T.)        // Pausa la lectura del teclado para poder procesar la tecla presionada
* PAUSE_READ_VK (.F.)        // Restablece la lectura del teclado luego de la pausa
*
*******************************************************************************************************
* Los valores virtuales (VK Code) de las diferentes teclas pueden encontrarlos en el archivo 
* C:\hmg.3.0.29\MINGW\include\winuser.h 
* 
* Comienzan con el prefijo #define VK_    
* Por ejemplo,  #define VK_SHIFT	16
*******************************************************************************************************


#include "minigui.ch"

Function Main
PRIVATE inicio

	DEFINE WINDOW Form_1 ;
		AT 0,0 ;
		WIDTH 400 ;
		HEIGHT 600 ;
		TITLE 'READ THE KEYBOARD' ;
		MAIN 
     
        @  10 , 10 LABEL Label_1 AUTOSIZE BOLD FONTCOLOR RED
        @  40 , 10 LABEL Label_2 AUTOSIZE BOLD FONTCOLOR RED
        @  80 , 10 LABEL Label_3 AUTOSIZE BOLD FONTCOLOR BLUE
        @ 120 , 10 LABEL Label_4 AUTOSIZE BOLD FONTCOLOR BLUE
        @ 160 , 10 LABEL Label_5 AUTOSIZE BOLD FONTCOLOR BLUE
        @ 200 , 10 LABEL Label_6 AUTOSIZE BOLD FONTCOLOR BROWN
        @ 240 , 10 LABEL Label_7 AUTOSIZE BOLD FONTCOLOR BROWN
        @ 280 , 10 LABEL Label_8 AUTOSIZE BOLD FONTCOLOR BROWN
    
        @ 430 , 10 LABEL Label_9 AUTOSIZE BOLD
       
        @ 380 , 10 LABEL Label_10 VALUE "Presionar SHIFT + A abre Caja Dialogo" AUTOSIZE BOLD
		
		@ 450 , 10 EDITBOX EditBox_1  WIDTH 200 HEIGHT 100 VALUE "Escribir algo" 

		IF INSTALL_READ_KEYBOARD () = .F.
		   MsgInfo ("ERROR al instalar READ_KEYBOARD")
		ELSE
		   DEFINE TIMER timer_1 OF Form_1 INTERVAL 100  action Mostrar_Tecla () 
		ENDIF
		
	//	IF UNINSTALL_READ_KEYBOARD () = .F.
	//	   MsgInfo ("ERROR al desinstalar READ_KEYBOARD")
	//  ELSE
	//     Form_1.Timer_1.Release
	//	ENDIF
				
	END WINDOW

	Form_1.Center 

	Form_1.Activate 

Return



Procedure Mostrar_Tecla
  Form_1.Label_1.Value := "VK Code: " + str (GET_LAST_VK())
  Form_1.Label_2.Value := "VK Name:            " + GET_LAST_VK_NAME()
  Form_1.Label_3.Value := "SHIFT presionado:     " + IF (GET_STATE_VK_SHIFT()=.T.,"Si","No")
  Form_1.Label_4.Value := "CONTROL presionado:   " + IF (GET_STATE_VK_CONTROL()=.T.,"Si","No")
  Form_1.Label_5.Value := "ALT presionado:       " + IF (GET_STATE_VK_ALT()=.T.,"Si","No")
  Form_1.Label_6.Value := "CAPS LOCK activado:   " + IF (IsCapsLockActive() =.T.,"Si","No") // HMG function
  Form_1.Label_7.Value := "NUM LOCK activado:    " + IF (IsNumLockActive()  =.T.,"Si","No") // HMG function
  Form_1.Label_8.Value := "INSERT activado:      " + IF (IsInsertActive() =.T.,"Si","No")   // HMG function

  IF (GET_STATE_VK_SHIFT()=.T. .OR. IsCapsLockActive() =.T.) .AND. GET_LAST_VK() <> 0
      Form_1.Label_9.Value := "Escribiendo en MAYUSCULAS"
  ELSE
      Form_1.Label_9.Value := "Escribiendo en MINISCULAS"
  ENDIF
  
  
  IF GET_LAST_VK() = 65  .AND. GET_STATE_VK_SHIFT()=.T.  //  VK code de A = 65
      PAUSE_READ_VK (.T.) // pausa la lectura del teclado
      Form_1.Timer_1.Enabled := .F.
       
      Msginfo ("Procesar la accion de la tecla SHIFT + A")
      
      Form_1.Timer_1.Enabled := .T.
      PAUSE_READ_VK (.F.) // restablece la lectura del teclado
  ENDIF
Return


*#########################################################################################################################
*   FUNCIONES EN C        
*#########################################################################################################################

#pragma begindump

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

BOOL flag_hhk = FALSE;
BOOL PAUSE_hhk = FALSE;
HHOOK hhk = NULL;
long VK_PRESIONADO = 0;
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

Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

User avatar
Vanguarda
Posts: 543
Joined: Wed Feb 11, 2009 10:56 am
Location: Americana - SP
Contact:

Post by Vanguarda » Wed Apr 14, 2010 2:27 am

Hi friend,

Very nice. Thanks for share with us.

My best regards,
--
Paulo Sérgio Durço (Vanguarda)


http://hmglights.wordpress.com/

User avatar
mol
Posts: 2759
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 97 times
Been thanked: 38 times
Contact:

Post by mol » Wed Apr 14, 2010 5:31 am

fine! thx!
Marek

User avatar
Rathinagiri
Posts: 5110
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 100 times
Been thanked: 108 times
Contact:

Post by Rathinagiri » Wed Apr 14, 2010 5:55 am

Cool! Can this be compared with our age old inkey()
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
gfilatov
Posts: 502
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Has thanked: 6 times
Been thanked: 32 times
Contact:

Post by gfilatov » Wed Apr 14, 2010 8:36 am

Hi Claudio,

Great sample!

Many thanks for sharing.
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein

User avatar
esgici
Posts: 4296
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 186 times
Been thanked: 68 times
Contact:

Post by esgici » Wed Apr 14, 2010 8:37 am

Nice !

Thanks Dr. :)

Saludos

--

Esgici
Viva INTERNATIONAL HMG :D

User avatar
esgici
Posts: 4296
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 186 times
Been thanked: 68 times
Contact:

Post by esgici » Wed Apr 14, 2010 9:24 am

Hola Dr

Perfecto, muchas gracias :)

I don't like low level approach in general, but I'd like very much this time :D

Saludos cordiales

--

Esgici
Viva INTERNATIONAL HMG :D

User avatar
srvet_claudio
Posts: 1919
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 25 times
Been thanked: 96 times
Contact:

Post by srvet_claudio » Wed Apr 14, 2010 12:58 pm

Hi Paulo Sérgio, Marek, Rathinagiri, Grigory, and Esgici.

Thank you very much for your comments.
Best regards,
Claudio Soto.
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

User avatar
esgici
Posts: 4296
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 186 times
Been thanked: 68 times
Contact:

Post by esgici » Wed Apr 14, 2010 2:07 pm

Hi Calculator makers (Vanguarda and Marek)

Do you think adding navigation / editing keys ( ins, del, BS, lefte, rigth etc) to your calculators after Claudio work ?

Regards

--

Esgici
Viva INTERNATIONAL HMG :D

User avatar
Alex Gustow
Posts: 290
Joined: Thu Dec 04, 2008 1:05 pm
Location: Yekaterinburg, Russia
Contact:

Post by Alex Gustow » Wed Apr 14, 2010 5:15 pm

Claudio, thanks! Good and very useful I mean!
But... can anybody translate comments etc in this to English (for better understanding and "internationalization")? I think it helps to many guys whose Spanish is "not the best" [like me :) ].

Post Reply