Link to home
Start Free TrialLog in
Avatar of kuclu
kuclu

asked on

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}")
Avatar of ericwong27
ericwong27
Flag of Singapore image

System.Windows.Forms.SendKeys.Send("^{ESC}");
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
Avatar of kuclu
kuclu

ASKER

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??

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?
Avatar of kuclu

ASKER

yes i want to open the start menu
kuclu,

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

Wayne
Avatar of kuclu

ASKER

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...
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
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
Avatar of kuclu

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Priest04
Priest04
Flag of Serbia 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
Avatar of kuclu

ASKER

Perfect!! That's exactly what i needed. thanks.
You are welcome

Goran