Equivalent of VB Sendkey

Equivalent of VB Sendkey

Postby Marco Turco » 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
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Equivalent of VB Sendkey

Postby James Bott » Wed Dec 03, 2014 1:44 pm

If you search the forum for "sendkey" you will find a number of postings. Perhaps one of those will help.
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Equivalent of VB Sendkey

Postby Rick Lipkin » Wed Dec 03, 2014 1: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
User avatar
Rick Lipkin
 
Posts: 2658
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Equivalent of VB Sendkey

Postby emotta_no » Wed Dec 03, 2014 2:12 pm

I made this function. It do this.


Code: Select all  Expand view

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

 
emotta_no
 
Posts: 33
Joined: Thu Jul 04, 2013 9:28 pm

Re: Equivalent of VB Sendkey

Postby Marco Turco » Wed Dec 03, 2014 2: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
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Equivalent of VB Sendkey

Postby Antonio Linares » Wed Dec 03, 2014 3: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
User avatar
Antonio Linares
Site Admin
 
Posts: 41898
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Equivalent of VB Sendkey

Postby Antonio Linares » Wed Dec 03, 2014 3: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: Select all  Expand view
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 41898
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Equivalent of VB Sendkey

Postby Marco Turco » 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
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Equivalent of VB Sendkey

Postby Antonio Linares » 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
User avatar
Antonio Linares
Site Admin
 
Posts: 41898
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Equivalent of VB Sendkey

Postby Marco Turco » 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
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Equivalent of VB Sendkey

Postby hmpaquito » Thu Dec 04, 2014 12:44 pm

Marco,

Try so:

Code: Select all  Expand view
function Main()
local hWnd

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




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



Regards
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: Equivalent of VB Sendkey

Postby Antonio Linares » Thu Dec 04, 2014 3: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
User avatar
Antonio Linares
Site Admin
 
Posts: 41898
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Equivalent of VB Sendkey

Postby Marco Turco » Thu Dec 04, 2014 5:07 pm

It seems ok now. Thank you all.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 36 guests