Help with Forms & Keyboard

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
MGOLDFARB
Posts: 71
Joined: Thu Jun 21, 2012 7:46 am

Help with Forms & Keyboard

Post by MGOLDFARB »

Hi.

I have a form that is displaying a record. I have buttons for next record, previous record, save etc.

My user would like the ability to push "N" for next record, "P" for previous record etc. Is there a way to trap the keyboard for the entire form, so I can call functions if certain keys are pressed?

Thanks.

Mark
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Help with Forms & Keyboard

Post by esgici »

MGOLDFARB wrote:Hi.

I have a form that is displaying a record. I have buttons for next record, previous record, save etc.

My user would like the ability to push "N" for next record, "P" for previous record etc. Is there a way to trap the keyboard for the entire form, so I can call functions if certain keys are pressed?

Thanks.

Mark
HMG_Doc wrote:ON KEY
Defines a Keyboard Shortcut

Code: Select all

      ON KEY <Key>
            [ OF <ParentWindow> ]
            ACTION <ActionProcedureName> | <bBlock>
<Key> must be one of the following:
...
But (due to some requisite reasons) ON KEY command doesn't support single letter keys. Instead you can use ALT , CONTROL and SHIFT key combinations.

Happy HMG'ing :D
Viva INTERNATIONAL HMG :D
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Help with Forms & Keyboard

Post by mol »

you can search calculator sample on this forum. it contains fine realised action on pressed numeric keys
MGOLDFARB
Posts: 71
Joined: Thu Jun 21, 2012 7:46 am

Re: Help with Forms & Keyboard

Post by MGOLDFARB »

Ok. I got this to work if I go into the form file (.FMG) created with the IDE program and stick the ON key lines in it. However, the next time I pulled the form up in the IDE form editor, these lines were removed.

The name of the form is ACTUAL_SCHEDULE_EDIT

These are the two lines I placed in the actual FMG file, and it worked.
on key NEXT ACTION actual_schedule_next()
on key PRIOR ACTION actual_schedule_previous()

In the code, I tried placing these lines both before and after the LOAD WINDOW Actual_Schedule_Edit line, and it did not work.
I tried changing the lines to reference the window as follows (and that also did not work).
on key NEXT OF Actual_schedule_edit ACTION actual_schedule_next()
on key PRIOR OF Actual_schedule_edit ACTION actual_schedule_previous()

Any idea what I am doing wrong?

Thank you!

Mark
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Help with Forms & Keyboard

Post by esgici »

Hi Mark
MGOLDFARB wrote: Any idea what I am doing wrong?
No idea, because we haven't a sample about what you are did ;)

But this sample work well, IMHO will help you :arrow:

Happy HMG'ing :D
Screen shoot for using ON KEY on .fmg
Screen shoot for using ON KEY on .fmg
OnKeyFmg.PNG (68.72 KiB) Viewed 3883 times
OnKeyFmg.zip
Source files ( .hbp, .prg, .fmg ) for test ON KEY on fmg
(1.02 KiB) Downloaded 271 times
Viva INTERNATIONAL HMG :D
MGOLDFARB
Posts: 71
Joined: Thu Jun 21, 2012 7:46 am

Re: Help with Forms & Keyboard

Post by MGOLDFARB »

got it to work. it was a careless mistake on my side... thanks for the assist!
Mark
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Help with Forms & Keyboard

Post by mol »

you can't place any code in .fmg file. It will be always removed by IDE
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Help with Forms & Keyboard

Post by mol »

I've found such a sample, maybe it will be useful for reading keyboard:

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 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"
		@ 450, 350 button P_1 caption "_Cokolwiek" action MsgBox("Wciśnięty...")
      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

	
		


Post Reply