FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Equivalent of VB Sendkey
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM
Equivalent of VB Sendkey
Posted: Wed Dec 03, 2014 11:15 AM

Hi all,
I'm looking for a function like the visual basic SendKey in order to send some Keys to a window process.
Do you know something like this ?
In alternative, do you know a way to put the focus on a Windows process ?

Thank you in advance

Best Regards,



Marco Turco

SOFTWARE XP LLP
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Equivalent of VB Sendkey
Posted: Wed Dec 03, 2014 01:44 PM

If you search the forum for "sendkey" you will find a number of postings. Perhaps one of those will help.

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Equivalent of VB Sendkey
Posted: Wed Dec 03, 2014 01:49 PM

Marco

Can you not use __KeyBoard() function ?? .. or possibly create a hidden button and put some code behind it and call it with oBtn:Click()

Rick Lipkin

Posts: 33
Joined: Thu Jul 04, 2013 09:28 PM
Re: Equivalent of VB Sendkey
Posted: Wed Dec 03, 2014 02:12 PM
I made this function. It do this.


Code (fw): Select all Collapse
Function TesteSend()

MySendText("TESTANDO")

Return



Static Function MySendText(cTexto)
Local nI
Local cCar
Local lShift

// sem shift
cTexto := StrTran(cTexto,".",Chr(190))
cTexto := StrTran(cTexto,"/",Chr(193))
cTexto := StrTran(cTexto,"=",Chr(187))


// com shift
cTexto := StrTran(cTexto,":",Chr(191))

If GetKeyToggle(20) // se capslock estiver ativo
   MySendKey(20,0)
   MySendKey(20,45)
EndIf

If Len(cTexto)==0
   MySendKey(17,0)  // control
   MySendKey(Asc(Upper("V")),0)
   MySendKey(Asc(Upper("V")),45)
   MySendKey(17,45)
   Return
EndIf

For nI := 1 to Len(cTexto)
   cCar := SubStr(cTexto,nI,1)
   lShift := IsUpper( cCar )
   If lShift
      MySendKey(16,0)
   EndIf
   MySendKey(Asc(Upper(cCar)),0)
   If lShift
      MySendKey(16,45)
   EndIf
Next

Return

#pragma begindump

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

#define WH_KEYBOARD_LL 13                                               


HB_FUNC ( EMTGETWINDOW )      // RETORNA A WINDOW QUE ESTA EM FOCO
{

    hb_retni(GetForegroundWindow());
}


HB_FUNC( MYSENDKEY ) 
{
int nPress;

nPress = hb_parni(2);

if (nPress == 0)
   keybd_event( hb_parni(1), nPress, KEYEVENTF_EXTENDEDKEY | 0, 0 ); 
else
   keybd_event( hb_parni(1), nPress, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 ); 

} 



#pragma enddump
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM
Re: Equivalent of VB Sendkey
Posted: Wed Dec 03, 2014 02:35 PM

Thank you all for the suggests but my main issue is not to send the key due there are several way to do this,
my issue is to get the hwnd container.

Essentially, having the name of the process (example "winapp") I need to send some Keys to this app from my app,
like the user is using the winapp and it is pressing some Keys to be clear.

Best Regards,



Marco Turco

SOFTWARE XP LLP
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Equivalent of VB Sendkey
Posted: Wed Dec 03, 2014 03:45 PM

Marco,

First of all you need to get the handle of the window where you are going to send the keys to.

You can get it from the caption of the main window of the app:

FindWindow( 0, "winapp" ) --> handle of the window

Now, once you have it you can send it any windows messages :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Equivalent of VB Sendkey
Posted: Wed Dec 03, 2014 03:48 PM
Marco,

If it is a control of "winapp", then you have to step through all its child controls until you find it:

Here you have an example looking for an "edit" child control:

Code (fw): Select all Collapse
#include "FiveWin.ch" 
#define GW_CHILD      5 
#define GW_HWNDNEXT   2 

FUNCTION Main() 
LOCAL oWnd, hWnd, hCtrl, cClassName, cTitle 
cTitle := PADR("TEST", 200) 
IF MsgGet( "Please write the title of a window", "Title:", @cTitle) 
   hWnd := FindWindow( 0, ALLTRIM(cTitle)) 
   IF hWnd <= 0 
      MsgInfo( "Sorry I can't find that window" ) 
   ELSE 
      hCtrl := GetWindow( hWnd, GW_CHILD ) 
      WHILE hCtrl != 0 
            cClassName := Upper(GetClassName(hCtrl)) 
            IF cClassName = "EDIT" 
               ? cClassName+"  "+GetWindowText(hCtrl) 
            ENDIF 
            hCtrl = GetWindow( hCtrl, GW_HWNDNEXT ) 
      END 
   ENDIF 
ENDIF 
RETURN NIL
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM
Re: Equivalent of VB Sendkey
Posted: Thu Dec 04, 2014 10:00 AM

Hi Antonio,
thank for your suggest, FindWindow is fine for my case

I'm trying to send some chars to the opened Notepad from my FWH app,
Findwindow return the notepad hWnd but using __keyboard() notepad doesn't receive the chars despite it has focus.
Any ideas ? Do I have to replace __keyboard with another function in this case ?

Thank you.


function Main()
local hWnd

hWnd:=FindWindow(0,"Noname - Notepad")
SetFocus(hWnd)
__keyboard("a")
return

Best Regards,



Marco Turco

SOFTWARE XP LLP
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Equivalent of VB Sendkey
Posted: Thu Dec 04, 2014 10:22 AM

Marco,

Use three underscores on the function name:

_ _ _keyboard( "Hello world" )

together :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM
Re: Equivalent of VB Sendkey
Posted: Thu Dec 04, 2014 10:34 AM

Nothing to do,
"Hello world" always go at the end of my fwh app on the Windows cmd prompt instead of notepad.
It seems like notepad doesn't receive the focus before I use ___keyboard

Best Regards,



Marco Turco

SOFTWARE XP LLP
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: Equivalent of VB Sendkey
Posted: Thu Dec 04, 2014 12:44 PM
Marco,

Try so:

Code (fw): Select all Collapse
function Main()
local hWnd

hWnd:=FindWindow(0,"Noname - Notepad")
SetForegroundWindowForce(hWnd)
__keyboard("a")
return




Code (fw): Select all Collapse
STATIC FUNCTION SetForegroundWindowForce(hWnd)
SetFocus(hWnd)                  
SendKey(VK_RETURN)          // The key
SetFocus(hWnd)                  
SetForegroundWindow(hWnd)  
SysRefresh()
RETURN NIL



Regards
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Equivalent of VB Sendkey
Posted: Thu Dec 04, 2014 03:35 PM

Marco,

Once that you have the handle of the control try to do this:

PostMessage( hWnd, WM_CHAR, Asc( "a" ) )

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM
Re: Equivalent of VB Sendkey
Posted: Thu Dec 04, 2014 05:07 PM

It seems ok now. Thank you all.

Best Regards,



Marco Turco

SOFTWARE XP LLP

Continue the discussion