Solved

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

Posted on 2004-03-31
8
2,037 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:kumarakrishnan
  • 4
  • 2
  • 2
8 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 125 total points
ID: 10723289
Use :

Shell "notepad.exe c:\yourfile.txt"
0
 
LVL 14

Expert Comment

by:aelatik
ID: 10723309
Private Sub Form_Load()
Shell "notepad.exe c:\yourfile.txt", vbMaximizedFocus
End Sub
0
 
LVL 14

Expert Comment

by:aelatik
ID: 10723383
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
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 1

Author Comment

by:kumarakrishnan
ID: 10723436
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.
0
 
LVL 4

Expert Comment

by:sokolovsky
ID: 10723442
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
0
 
LVL 14

Expert Comment

by:aelatik
ID: 10723497
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 )
0
 
LVL 1

Author Comment

by:kumarakrishnan
ID: 10723625
Thankyou Aelatik,

it worked.

Kumara Krishnan
0
 
LVL 4

Expert Comment

by:sokolovsky
ID: 10723664
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
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question