PDA

View Full Version : Send Keys kludge



v_gyku
10-21-2005, 03:32 AM
Anyone has idea what send keys kludge is?

I want to click ok button of the dialog box with send keys kludge.....

THANKS...

Bob Phillips
10-21-2005, 03:49 AM
Anyone has idea what send keys kludge is?

I want to click ok button of the dialog box with send keys kludge.....


The Sendkeys kludge is the nasty way of simulating a button press by using sendkeys to send a keystroke to the activewindow. For example, try this


Application.SendKeys "~"
MsgBox "Yes or no", vbYesNo


You might see something flash up, but you don't get a chance to respond to the MsgBox.

andysuth
08-01-2007, 04:30 AM
Can I use this function to send a key combination like "<ctrl><p>" and "<return>" to a third party application without any macro abilities in it, if it is open at the time?

Cheers,

-Andy

Bob Phillips
08-01-2007, 04:33 AM
Not without code I wouldn't think.

andysuth
08-01-2007, 04:45 AM
I'm getting a "Notepad" window with just the letter "H" in it with this Code:


Sub hello()
Dim keystogo As String
keystogo = "Hello Notepad"
Shell "c:\windows\notepad.exe", vbMaximizedFocus
Application.SendKeys keystogo
End Sub


Surely that's not too far from what I want?

Is it possible to retain focus on the external window whilst sending Key strokes?

-AS

rory
08-01-2007, 04:49 AM
Do you know the caption of the window you want to send the commands to?

Bob Phillips
08-01-2007, 04:53 AM
This works

Dim keystogo As String
Dim i As Long
keystogo = "Hello Notepad"
Shell "c:\windows\notepad.exe", vbMaximizedFocus
For i = 1 To Len(keystogo)
Application.SendKeys Mid$(keystogo, i, 1)
Application.Wait Now() + TimeSerial(0, 0, 1)
Next i

andysuth
08-01-2007, 04:56 AM
Where is the caption kept?

On the mouseover on the tool bar a yellow text box window comes up that says:

"Solid Edge 2D Drafting V19 - Draft - [DOOR_001.dft]"

Same as the title bar at top of window when maximised.

Same as Task Manager.

I basically want to send the key strokes:
<CTRL> <P> 'to start print wizzard
<RETURN> 'to print default printer (CutePDF)
<RETURN> 'to print to default file name, default file type, default location
<ALT><F4> 'Exit
<RETURN> 'Exit ok.

If you get me the simple codes for how to send to the application, I can sort the specific keystrokes out.

Thanks,

-Andy.

andysuth
08-01-2007, 05:00 AM
Xid,

Thanks, it does work, it's also quite scarey, and I'm the one who put it on the computer.

I'm thinking of the April 1st applications I can use this for, maybe a little more random timing on the delay and a more synister message!

I'll let you know what happens with the SolidEdge 2D package.

Would it be ok to post a link to the SE download on this thread?

-AS

andysuth
08-01-2007, 05:01 AM
Do you know how to send a <CTRL> or <ALT> to an application?

Cheers,

-AS

andysuth
08-01-2007, 05:28 AM
I got the "^p" thing, that works well in Notepad, but not in SolidEdge,

As I've got to open the window first (up to 20 second) I've added a pause function, but it now focuses in the wrong window.

Rory, did your method for which you asked for the window name refocus on the window, because this might solve my problem.

Cheers,

-Andy

andysuth
08-01-2007, 05:33 AM
I've got this, but unfortunately, as CutePDF is a secondary shell, I need to refocus on that between the two "~".

Any ideas?


Sub SE2D()
Shell "C:\SE2D\Program\Edge.exe C:\PE3\AR\DOOR_001.dft", vbMaximizedFocus
Application.Wait Now() + TimeSerial(0, 0, 10)
Application.SendKeys "^p", True
Application.SendKeys "~", True
Application.SendKeys "~", True
End Sub


Cheers,

-AS

rory
08-01-2007, 06:46 AM
Here you go - change the caption to match yours:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" _
(ByVal cChar As Byte) As Integer
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _
(ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Const KEYEVENTF_KEYUP = &H2
Private Const vbKeyShift = &H10
Private Const vbKeyCtrl = &H11
Private Const vbKeyAlt = &H12
Const VK_RETURN As Integer = &HD
Const SW_HIDE = 0

Private Sub PrintNotepad()
Dim hWnd As Long, ForeWin As Long
ForeWin = GetForegroundWindow()
' Change this to match your caption
hWnd = FindHwnd("Untitled - Notepad")
If hWnd = 0 Then Exit Sub
SetForegroundWindow hWnd
Call Send_Keys("this is some text", hWnd)
keybd_event vbKeyCtrl, 0, 0, 0
Call Send_Keys("p", hWnd)
keybd_event vbKeyCtrl, 0, KEYEVENTF_KEYUP, 0
' Send Return


keybd_event VK_RETURN, 0, 0, 0
keybd_event VK_RETURN, 0, KEYEVENTF_KEYUP, 0
SetForegroundWindow ForeWin
End Sub
Public Sub Send_Keys(KeysToSend As String, WindowToSendTo As Long)
Dim ShiftDown As Boolean, AltDown As Boolean, ControlDown As Boolean
Dim intCount As Integer
Dim mScan As Long
Dim a As Integer
Dim mVK As Long

If KeysToSend = "" Then Exit Sub
If WindowToSendTo = 0 Then Exit Sub

For a = 1 To Len(KeysToSend)

mVK = VkKeyScan(Asc(Mid(KeysToSend, a, 1)))
mScan = MapVirtualKey(mVK, 0)

ShiftDown = (mVK And &H100)
ControlDown = (mVK And &H200)
AltDown = (mVK And &H400)

mVK = mVK And &HFF


' Do While GetForegroundWindow() <> WindowToSendTo And intCount < 20
' DoEvents
' intCount = intCount + 1
' SetForegroundWindow WindowToSendTo
' Loop
'
' If GetForegroundWindow() <> WindowToSendTo Then Exit Sub

If ShiftDown Then keybd_event &H10, 0, 0, 0
If ControlDown And &H200 Then keybd_event &H11, 0, 0, 0
If AltDown And &H400 Then keybd_event &H12, 0, 0, 0

keybd_event mVK, mScan, 0, 0

If ShiftDown Then keybd_event vbKeyShift, 0, KEYEVENTF_KEYUP, 0
If ControlDown Then keybd_event vbKeyCtrl, 0, KEYEVENTF_KEYUP, 0
If AltDown Then keybd_event vbKeyAlt, 0, KEYEVENTF_KEYUP, 0

Next a

' SetForegroundWindow Application.hWnd

End Sub


Private Function FindHwnd(strCaption As String) As Long
'Returns the handle of the specified window
FindHwnd = FindWindow(vbNullString, strCaption)
End Function


HTH
Rory

rory
08-01-2007, 06:47 AM
PS That can probably be simplified a bit for what you are trying to do - you don't really need the SendKeys routine I suspect.

andysuth
08-01-2007, 06:49 AM
Nice dude, I'll be butchering in a few minutes!!!!

Bob Phillips
08-01-2007, 07:15 AM
Do you still need this, or have you picked the response from the other thread?

andysuth
08-01-2007, 07:17 AM
I think I'm about done on all of the threads, I'll post the final code once tested, currently it's a bit flighty, relying on the focus following natural order, but I'm almost there.

-AS

andysuth
08-02-2007, 05:22 AM
Any one know the code for send key down arrow?

Cheers,

-AS

Bob Phillips
08-02-2007, 05:30 AM
{DOWN}