• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 727
  • Last Modified:

simulating ctrl + esc keystrokes

Hi,
I want to simulate the ctrl + esc keystrokes using Sendkeys.SendWait in VB.NET, but nothing is happening. Below is my code. Can someone please tell me what am i doing wrong. thanks.

Imports System.Windows.Forms

SendKeys.SendWait("^{ESC}")
0
kuclu
Asked:
kuclu
  • 5
  • 3
  • 3
  • +2
1 Solution
 
ericwong27Commented:
System.Windows.Forms.SendKeys.Send("^{ESC}");
0
 
Wayne Taylor (webtubbs)Commented:
Hello kuclu,

It works fine for me. You'll need to ensure that the control/form/whatever has the focus in order to capture SendKeys.

You might also like to try SendKeys.Send("^{ESC}")

Regards,

Wayne
0
 
kucluAuthor Commented:
hi wayne,

i have a filesystemwatcher and i want it to trigger ctrl+esc when a certain event happens. So actually i do not need to set focus on anything, right? the filesystemwatcher works fine. all i need is to figure out how to simulate ctrl+esc. what do you think is going wrong??

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Wayne Taylor (webtubbs)Commented:
What exactly would you like Ctrl+ESC to do?

I did a quick test, with a simple form (with KeyPreview = TRUE) with only a button, and these events....

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        MsgBox(IIf(e.Control, "Ctrl+", "") & e.KeyCode.ToString)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SendKeys.SendWait("^{ESC}")
    End Sub

Clicking the button registered the keyup event, but manually pressing Ctrl+ESC opens up the start menu. Is this the desired effect?
0
 
kucluAuthor Commented:
yes i want to open the start menu
0
 
Wayne Taylor (webtubbs)Commented:
kuclu,

Why do you want to show the start menu? I can't think of a valid reason for doing so.

Wayne
0
 
kucluAuthor Commented:
well, actually the reason is that i am launching a program from a filesystemwatcher, and this program (wxclips) is crashing, and when i manually press ctrl+esc (or ctrl+alt+del) the program resumes normally!

(now if the program is launched manually rather than automatically from the filesystemwatcher, it runs fine)
 
i thought that it is just a matter of figuring out how to simluate the ctrl+esc keys (or ctrl+alt+del). but i guess it is getting more complicated...
0
 
SanclerCommented:
Whoa!

I am not one of those who says you should never use sendkeys because you don't know where they might end up.  But even I find the thought of sending out _control_ sequences like this, without knowing precisely what has the focus and so will receive them, a bit frightening.

What you're looking for is clearly a "workaround".  The ideal solution would be to find a solution to the basic problem - that the wxclips program is crashing - rather than working around it.  But if that's not possible I strongly recommend that you base any workaround on other than using sendkeys what looks suspiciously like "at random".

And, pragmatically, the very fact that the sendkeys approach is not working tends to reinforce that "in principle" view.

Roger
0
 
Priest04Commented:
SendKeys will send keys to active window, and Start button isnt in this case. To simulate CTRL+ESC you would need to use Win32 API

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
          ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    Private Const KEYEVENTF_KEYUP = &H2
    Private Const VK_CONTROL = &H11
    Private Const VK_ESCAPE = &H1B

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Press the Ctrl-Esc key
        keybd_event(VK_CONTROL, 0, 0, 0)
        keybd_event(VK_ESCAPE, 0, 0, 0)
        Application.DoEvents()
        ' Release the two keys
        keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
        keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0)
        Application.DoEvents()
    End Sub

Goran
0
 
kucluAuthor Commented:
Hi Priest04. thanks for your reply. I think your code may help me achieve what i want, but i still require some help, as i am new to this. Below is my code that i am using to watch a certain folder, and then launch the program. Now how can i use your code in my Windows Service that I created in VB.NET? Below is my code..


Imports System.ServiceProcess
Imports System.IO
Imports System.Threading.Thread
Imports System.Windows.Forms
Imports System.Diagnostics

Public Class Service1

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub

    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

        Dim proc As New Process
        proc = Process.Start("C:\Program Files\wxclips\wxclips.exe")

    ''''' i need to send ctrl+esc here


       End Sub
End Class
0
 
Priest04Commented:
Public Class Service1

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
          ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    Private Const KEYEVENTF_KEYUP = &H2
    Private Const VK_CONTROL = &H11
    Private Const VK_ESCAPE = &H1B

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub

    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

        Dim proc As New Process
        proc = Process.Start("C:\Program Files\wxclips\wxclips.exe")

    ''''' i need to send ctrl+esc here
' Press the Ctrl-Esc key
        keybd_event(VK_CONTROL, 0, 0, 0)
        keybd_event(VK_ESCAPE, 0, 0, 0)
        Application.DoEvents()
        ' Release the two keys
        keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
        keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0)
        Application.DoEvents()


       End Sub
End Class

Goran
0
 
kucluAuthor Commented:
Perfect!! That's exactly what i needed. thanks.
0
 
Priest04Commented:
You are welcome

Goran
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now