FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Intercept pressing VK_SNAPSHOT ( "PrtScr" )
Posts: 124
Joined: Mon Nov 14, 2005 10:15 AM
Intercept pressing VK_SNAPSHOT ( "PrtScr" )
Posted: Wed Jul 03, 2024 07:32 PM

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

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )
Posted: Thu Jul 04, 2024 05:00 AM
Dear Mauricio,

Here you have a working example:
Code (fw): Select all Collapse
#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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 124
Joined: Mon Nov 14, 2005 10:15 AM
Re: Intercept pressing VK_SNAPSHOT ( &quot;PrtScr&quot; )
Posted: Thu Jul 04, 2024 11:23 AM
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
Posts: 124
Joined: Mon Nov 14, 2005 10:15 AM
Re: Intercept pressing VK_SNAPSHOT ( &quot;PrtScr&quot; )
Posted: Thu Jul 04, 2024 11:36 AM

Or, how do I send this key pressing event to the :bKeyDown block of an object ?

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Intercept pressing VK_SNAPSHOT ( &quot;PrtScr&quot; )
Posted: Thu Jul 04, 2024 11:39 AM
Code (fw): Select all Collapse
#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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Intercept pressing VK_SNAPSHOT ( &quot;PrtScr&quot; )
Posted: Thu Jul 04, 2024 11:47 AM
> Or, how do I send this key pressing event to the :bKeyDown block of an object ?
Code (fw): Select all Collapse
#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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 124
Joined: Mon Nov 14, 2005 10:15 AM
Re: Intercept pressing VK_SNAPSHOT ( &quot;PrtScr&quot; )
Posted: Thu Jul 04, 2024 12:35 PM

Thanks Antonio!

[[]]

Continue the discussion