Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Postby concentra » Wed Jul 03, 2024 7: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
User avatar
concentra
 
Posts: 124
Joined: Mon Nov 14, 2005 10:15 am
Location: Brazil

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Postby Antonio Linares » Thu Jul 04, 2024 5:00 am

Dear Mauricio,

Here you have a working example:
Code: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Postby concentra » 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
User avatar
concentra
 
Posts: 124
Joined: Mon Nov 14, 2005 10:15 am
Location: Brazil

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Postby concentra » Thu Jul 04, 2024 11:36 am

Or, how do I send this key pressing event to the :bKeyDown block of an object ?
User avatar
concentra
 
Posts: 124
Joined: Mon Nov 14, 2005 10:15 am
Location: Brazil

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Postby Antonio Linares » Thu Jul 04, 2024 11:39 am

Code: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Postby Antonio Linares » Thu Jul 04, 2024 11:47 am

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

Code: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Intercept pressing VK_SNAPSHOT ( "PrtScr" )

Postby concentra » Thu Jul 04, 2024 12:35 pm

Thanks Antonio!

[[]]
User avatar
concentra
 
Posts: 124
Joined: Mon Nov 14, 2005 10:15 am
Location: Brazil


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: MaxP and 51 guests