Page 1 of 1

Equivalent of VB Sendkey

Posted: Wed Dec 03, 2014 11:15 am
by Marco Turco
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

Re: Equivalent of VB Sendkey

Posted: Wed Dec 03, 2014 1:44 pm
by James Bott
If you search the forum for "sendkey" you will find a number of postings. Perhaps one of those will help.

Re: Equivalent of VB Sendkey

Posted: Wed Dec 03, 2014 1:49 pm
by Rick Lipkin
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

Re: Equivalent of VB Sendkey

Posted: Wed Dec 03, 2014 2:12 pm
by emotta_no
I made this function. It do this.


Code: Select all | Expand


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

 

Re: Equivalent of VB Sendkey

Posted: Wed Dec 03, 2014 2:35 pm
by Marco Turco
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.

Re: Equivalent of VB Sendkey

Posted: Wed Dec 03, 2014 3:45 pm
by Antonio Linares
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 :-)

Re: Equivalent of VB Sendkey

Posted: Wed Dec 03, 2014 3:48 pm
by Antonio Linares
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: Select all | Expand

#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

Re: Equivalent of VB Sendkey

Posted: Thu Dec 04, 2014 10:00 am
by Marco Turco
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

Re: Equivalent of VB Sendkey

Posted: Thu Dec 04, 2014 10:22 am
by Antonio Linares
Marco,

Use three underscores on the function name:

_ _ _keyboard( "Hello world" )

together :-)

Re: Equivalent of VB Sendkey

Posted: Thu Dec 04, 2014 10:34 am
by Marco Turco
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

Re: Equivalent of VB Sendkey

Posted: Thu Dec 04, 2014 12:44 pm
by hmpaquito
Marco,

Try so:

Code: Select all | Expand

function Main()
local hWnd

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




Code: Select all | Expand

STATIC FUNCTION SetForegroundWindowForce(hWnd)
SetFocus(hWnd)                  
SendKey(VK_RETURN)          // The key
SetFocus(hWnd)                  
SetForegroundWindow(hWnd)  
SysRefresh()
RETURN NIL
 



Regards

Re: Equivalent of VB Sendkey

Posted: Thu Dec 04, 2014 3:35 pm
by Antonio Linares
Marco,

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

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

Re: Equivalent of VB Sendkey

Posted: Thu Dec 04, 2014 5:07 pm
by Marco Turco
It seems ok now. Thank you all.