?
Solved

simulating ctrl + esc keystrokes

Posted on 2007-07-31
13
Medium Priority
?
725 Views
Last Modified: 2013-11-26
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
Comment
Question by:kuclu
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 10

Expert Comment

by:ericwong27
ID: 19598817
System.Windows.Forms.SendKeys.Send("^{ESC}");
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 19599177
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
 

Author Comment

by:kuclu
ID: 19599275
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 19599306
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
 

Author Comment

by:kuclu
ID: 19599315
yes i want to open the start menu
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 19599409
kuclu,

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

Wayne
0
 

Author Comment

by:kuclu
ID: 19599448
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
 
LVL 34

Expert Comment

by:Sancler
ID: 19599844
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
 
LVL 18

Expert Comment

by:Priest04
ID: 19599914
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
 

Author Comment

by:kuclu
ID: 19604965
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
 
LVL 18

Accepted Solution

by:
Priest04 earned 1600 total points
ID: 19605359
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
 

Author Comment

by:kuclu
ID: 19606883
Perfect!! That's exactly what i needed. thanks.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19606905
You are welcome

Goran
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 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