Page 1 of 1

Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Posted: Wed Jul 03, 2024 7:32 pm
by concentra
Hi.
I would like to intercept the "PrtScrn" key when it's pressed by the user.
I suppose that the key code is VK_SNAPSHOT, please correct me if I am wrong.
But seems that Windows intercepts it first and FiveWin don't get that it was pressed.
Does anyone knows if is it possible ?

[[]]
Maurício Ventura Faria

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Posted: Thu Jul 04, 2024 5:00 am
by Antonio Linares
Dear Mauricio,

Here you have a working example:

Code: Select all | Expand

#include "Fivewin.ch"

function Main()

     local oDlg 

     DisablePrintScreen()

     DEFINE DIALOG oDlg 

     ACTIVATE DIALOG oDlg CENTERED

return nil

#pragma BEGINDUMP

#include <windows.h>

LRESULT CALLBACK KeyboardHookProc( int nCode, WPARAM wParam, LPARAM lParam )
{
    if (nCode == HC_ACTION) 
    {
        KBDLLHOOKSTRUCT * p = ( KBDLLHOOKSTRUCT * ) lParam;

        if( p->vkCode == VK_SNAPSHOT ) // Print Screen key
        {
           MessageBeep( -1 );
           return 1; 
        }   
    }
    return CallNextHookEx( NULL, nCode, wParam, lParam );
}

HB_FUNC( DISABLEPRINTSCREEN )
{
   HHOOK hHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0 );

   if( ! hHook ) 
      MessageBox( 0, "can't hook", "warning", 0 );
}

#pragma ENDDUMP

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Posted: Thu Jul 04, 2024 11:23 am
by concentra
Hi Antonio.

Thanks, your sample works.
But sorry, I am not a C guy...
MessageBeep( -1 ); ==> MyHbFunc()
How do I call a Harbour function, for example MyHbFunc() from the C code ?

[[]] Maurício Faria

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Posted: Thu Jul 04, 2024 11:36 am
by concentra
Or, how do I send this key pressing event to the :bKeyDown block of an object ?

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Posted: Thu Jul 04, 2024 11:39 am
by Antonio Linares

Code: Select all | Expand

#include "Fivewin.ch"

static oDlg

function Main()

     DisablePrintScreen()

     DEFINE DIALOG oDlg 

     ACTIVATE DIALOG oDlg CENTERED

return nil

function MyHbFunc()

   static nTimes := 0

   oDlg:SetText( "PrintScreen pressed " + Str( ++nTimes ) )

return nil    

#pragma BEGINDUMP

#include <windows.h>
#include <hbvm.h>

LRESULT CALLBACK KeyboardHookProc( int nCode, WPARAM wParam, LPARAM lParam )
{
    if( nCode == HC_ACTION ) 
    {
        KBDLLHOOKSTRUCT * p = ( KBDLLHOOKSTRUCT * ) lParam;

        if( p->vkCode == VK_SNAPSHOT ) // Print Screen key
        {
           hb_vmPushSymbol( hb_dynsymSymbol( hb_dynsymFindName( "MYHBFUNC" ) ) );
           hb_vmPushNil();
           hb_vmDo( 0 );
           return 1; 
        }   
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

HB_FUNC( DISABLEPRINTSCREEN )
{
   HHOOK hHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0 );

   if( ! hHook ) 
      MessageBox( 0, "can't hook", "warning", 0 );
}

#pragma ENDDUMP

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Posted: Thu Jul 04, 2024 11:47 am
by Antonio Linares
> Or, how do I send this key pressing event to the :bKeyDown block of an object ?

Code: Select all | Expand

#include "Fivewin.ch"

static oDlg

function Main()

     DisablePrintScreen()

     DEFINE DIALOG oDlg 

     oDlg:bKeyDown = { | nKey | If( nKey == VK_SNAPSHOT, MsgBeep(),) }

     ACTIVATE DIALOG oDlg CENTERED

return nil

function MyHbFunc( nKeyDown )

   Eval( oDlg:bKeyDown, nKeyDown )

return nil    

#pragma BEGINDUMP

#include <windows.h>
#include <hbvm.h>

LRESULT CALLBACK KeyboardHookProc( int nCode, WPARAM wParam, LPARAM lParam )
{
    if( nCode == HC_ACTION ) 
    {
        KBDLLHOOKSTRUCT * p = ( KBDLLHOOKSTRUCT * ) lParam;

        if( wParam == WM_KEYDOWN && p->vkCode == VK_SNAPSHOT ) // Print Screen key on keydown
        {
           hb_vmPushSymbol( hb_dynsymSymbol( hb_dynsymFindName( "MYHBFUNC" ) ) );
           hb_vmPushNil();
           hb_vmPushInteger( VK_SNAPSHOT );
           hb_vmDo( 1 );
           return 1; 
        }   
    }
    return CallNextHookEx( NULL, nCode, wParam, lParam );
}

HB_FUNC( DISABLEPRINTSCREEN )
{
   HHOOK hHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0 );

   if( ! hHook ) 
      MessageBox( 0, "can't hook", "warning", 0 );
}

#pragma ENDDUMP

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Posted: Thu Jul 04, 2024 12:35 pm
by concentra
Thanks Antonio!

[[]]