Link to home
Start Free TrialLog in
Avatar of kumarakrishnan
kumarakrishnan

asked on

sendkeys ^{ESC} (Control key + Escape key) not working

Hi,

Im using VB6, my OS is XP.

Basically i want to bringup an existing file (say .txt or .xls file) and wish to perform some keystrokes in them. I use sendkeys function to send keystrokes to the file.
When i open the file manually by double clicking on it, then everything works fine.

Now, I want to bringup this file automatically and send keystrokes to it.

I thaught thar giving the full file path and name in START>RUN then the file might come up.
So i sent keystroke ^{ESC} (Control Escape) to invoke the START menu and then sentkey "R" to invoke RUN. But this operation is not working.

Can anyone help, y this sendkeys ^{ESC} is not working? or is there any other way to bringup the file which i need.

Thanks,
Kumara Krishnan.
ASKER CERTIFIED SOLUTION
Avatar of aelatik
aelatik
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Private Sub Form_Load()
Shell "notepad.exe c:\yourfile.txt", vbMaximizedFocus
End Sub
Or use api to launch any extension with its associated program.

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Form_Load()
ShellExecute Me.hwnd, "open", "c:\out.txt", 0, "c:\", 3
End Sub
Avatar of kumarakrishnan
kumarakrishnan

ASKER

Ya this is working, but when i try to send key ctrl esc why the start menu is not invoked? any suggesion on this? I need to go into start programs and click on some app.
Use this:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1
Private Sub Form_Load()
    Dim strMyFile As String
    strMyFile = "c:\eula.txt" 'Path to your file
    ShellExecute Me.hwnd, "open", strMyFile, vbNullString, "C:\", SW_SHOWNORMAL
End Sub
Using sendkeys is very tricky ( and messy ).
it requires windows to focus on the target you wish to send keystrokes to.
I cannot explain why it doesn't works. ( I never use it because of that )
Thankyou Aelatik,

it worked.

Kumara Krishnan
If you still need to open Start menu and send keys, use this:

'Simple Form
Const VK_CONTROL = &H11
Const VK_ESCAPE = &H1B
Const KEYEVENTF_KEYUP = &H2
Const INPUT_MOUSE = 0
Const INPUT_KEYBOARD = 1
Const INPUT_HARDWARE = 2
Private Type MOUSEINPUT
  dx As Long
  dy As Long
  mouseData As Long
  dwFlags As Long
  time As Long
  dwExtraInfo As Long
End Type
Private Type KEYBDINPUT
  wVk As Integer
  wScan As Integer
  dwFlags As Long
  time As Long
  dwExtraInfo As Long
End Type
Private Type HARDWAREINPUT
  uMsg As Long
  wParamL As Integer
  wParamH As Integer
End Type
Private Type GENERALINPUT
  dwType As Long
  xi(0 To 23) As Byte
End Type
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub Form_Activate()
    Call OpenStart
    DoEvents
    SendKeys "p", True 'If you have "Programs group"
End Sub
Sub OpenStart()
    Dim GInput(0 To 3) As GENERALINPUT
    Dim KInput As KEYBDINPUT
    KInput.wVk = VK_CONTROL 'the key we're going to press
    KInput.dwFlags = 0 'press the key
    GInput(0).dwType = INPUT_KEYBOARD   ' keyboard input
    CopyMemory GInput(0).xi(0), KInput, Len(KInput)
    KInput.wVk = VK_ESCAPE  ' the key we're going to realease
    'KInput.dwFlags = KEYEVENTF_KEYUP  ' release the key
    KInput.dwFlags = 0  ' release the key
    GInput(1).dwType = INPUT_KEYBOARD  ' keyboard input
    CopyMemory GInput(1).xi(0), KInput, Len(KInput)
   
    KInput.wVk = VK_CONTROL 'the key we're going to press
    KInput.dwFlags = KEYEVENTF_KEYUP 'press the key
    GInput(2).dwType = INPUT_KEYBOARD   ' keyboard input
    CopyMemory GInput(2).xi(0), KInput, Len(KInput)
    KInput.wVk = VK_ESCAPE  ' the key we're going to realease
    KInput.dwFlags = KEYEVENTF_KEYUP  ' release the key
    GInput(3).dwType = INPUT_KEYBOARD  ' keyboard input
    CopyMemory GInput(3).xi(0), KInput, Len(KInput)
    'send the input now
    Call SendInput(4, GInput(0), Len(GInput(0)))
End Sub
Private Sub SendKey(bKey As Byte)
    Dim GInput(0 To 1) As GENERALINPUT
    Dim KInput As KEYBDINPUT
    KInput.wVk = bKey  'the key we're going to press
    KInput.dwFlags = 0 'press the key
    'copy the structure into the input array's buffer.
    GInput(0).dwType = INPUT_KEYBOARD   ' keyboard input
    CopyMemory GInput(0).xi(0), KInput, Len(KInput)
    'do the same as above, but for releasing the key
    KInput.wVk = bKey  ' the key we're going to realease
    KInput.dwFlags = KEYEVENTF_KEYUP  ' release the key
    GInput(1).dwType = INPUT_KEYBOARD  ' keyboard input
    CopyMemory GInput(1).xi(0), KInput, Len(KInput)
    'send the input now
    Call SendInput(2, GInput(0), Len(GInput(0)))
End Sub