Windows Develop Bookmark and Share   
 index > Windows Forms General > Sending keystrokes...
 

Sending keystrokes...

I'm trying to emulate sending a "CTRL-N" to a form in a Windows app after checking for a previous instance of the application. Once I determine if the users has the existing application up, I activate the current window, send a keystroke and then close the app that had the previous instance when launched. When the AppActivate runs, I can see that its setting focus to the app, but the control key combo doesn't work. If I do it manually, it works fine. Here's my code:


        If blnPrevInstance Then 'Activate Main Form window of existing app
            .
            .
            .
            Try
                AppActivate("FiberNet Reporting")  'This is the name of the title bar in the form that is already active
                SendKeys.Send("^N") ' I also tried SendKeys.Send("^(N)")
            Catch exException As ArgumentException
                .
                .
                .
            Finally
                Me.Visible = False
                CloseForm()
                Cursor.Current = System.Windows.Forms.Cursors.Default
                Application.Exit()
            End Try
         End If

Why is it not working???
MigrationUser 1  Monday, February 17, 2003 4:56 PM
Have you tried brackets around the ^ ?  

From the Visual Studio .NET documentation (SendKeys class):

"The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use {+}. "

I haven't done this in .net yet myself, so it's just a quickie answer to see if this will solve your problem.

julie

MigrationUser 1  Monday, February 17, 2003 6:06 PM
Thanks Julie, but I tried that as well (placing the ^ within braces) and still didn't work. I really wonder if the active application has focus or not in order to retrieve the key strokes. According to the AppActivate, it does. I even tried making it a processID which I retrieved from shelling out and still didn't work. I then tried changing it to "F2" for instance without any special characters to see if that would help and would not.
MigrationUser 1  Monday, February 17, 2003 8:48 PM
Bill-

Well, now you have intrigued me with this problem!

If these are both .net applications, i.e. both managed code, you should be able to get both apps to talk to each other.

Probably when you manually hit the control-N keys, windows is the first to catch that and sends it to the active application. But when you do the SendKeys from one application domain, it doesn't know that you want to send the keystrokes OUTSIDE of the domain, and therefore it doesn't have a clue about what is the "active application".

I have not done any work with application domains yet, but here is a resource that might be useful

http://www.gotdotnet.com/team/clr/appdomainfaq.aspx

Of course,I can't help wondering if the active app that you are trying to send the keys to is the VB6 app that you were talking about in a different thread. In that case, I believe there are still means within the managed code to talk to the unmanaged (VB6) code.

Hopefully, someone else will come along to this thread and be able to tell you a few simple lines of code to accomplish this task. But until then, you might want to dig around in the app domain stuff.

Man, there is just TOO much to learn in .NET!!!

julie
MigrationUser 1  Monday, February 17, 2003 10:20 PM
sorry for this OT post, but I had to...

<quote>
Man, there is just TOO much to learn in .NET!!!
</quote>

Amen!  That's what makes it so much fun!  :) 
MigrationUser 1  Monday, February 17, 2003 10:27 PM
Julie, both applications are from within .Net. Here is the scenario:

1) The .Net application is already active.

2) The user issues a command to start the application again. If the application is already active, I then try to activate the other application, issue the Sendkeys command and then close the application that they tried to start. 

I can't get it to work..........
MigrationUser 1  Tuesday, February 18, 2003 8:39 AM
So, if the goal is to close the active application, can you use ALT-F4 instead of CTRL-N? I can get that key combo to do the trick. I know you already tried the corresponding CTRL-N 
"^(N)", so there is no question about that. I just don't know what is capturing the CTRL-N. Have you tried to sendkeys CTRL-N to make the application close itself?

Well, here is what did work.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    AppActivate("AppDomainTest AppTwo")
    SendKeys.Send("%({F4})")
End Sub

??

julie
MigrationUser 1  Tuesday, February 18, 2003 10:08 AM
Julie, I used the ASCII conversion for the "CTRL-N" combination and worked fine:

SendKeys.Send(Chr(14)) 

MigrationUser 1  Tuesday, February 18, 2003 10:40 AM
yeah. But now, I know how to work with appactivate, I have learned a bunch of tricks with sendkeys and I have a better understanding of appdomains than I did yesterday!

I knew there was going to be a "keep it simple/stupid" solution to what you were trying to do. 

julie
MigrationUser 1  Tuesday, February 18, 2003 10:52 AM
Another problem resurfaced with this. The logic to send the info over to the form via "SendKeys" works fine --- only if the form that I'm trying to send the info to is ALREADY MAXIMIZED. It doesn't need to have focus, just so long as it is maximized. If the form is minimized, it doesn't work.

Here is my code:

If blnPrevInstance Then 'Activate Main Form window of existing app
            Dim strErrMsg As String
            Dim strAppName As String = "FiberNet Reporting"
            Dim blnWeb As Boolean = False
            Dim i As Integer
            Try
                Me.Visible = False
                CloseForm()
                Cursor.Current = System.Windows.Forms.Cursors.Default
                AppActivate("FiberNet Reporting")
                Select Case strAutomatedMenuItem
                    Case "A"
                        SendKeys.Send(Chr(1))
                    Case "N"
                        SendKeys.Send(Chr(14))
                    Case "F"
                        SendKeys.Send(Chr(6))
                    Case "Y"
                        SendKeys.Send(Chr(25))
                    Case "L"
                        SendKeys.Send(Chr(12))
                    Case "S"
                        SendKeys.Send(Chr(19))
                End Select
            Catch exException As ArgumentException
                Dim strBuilder As New StringBuilder()
                Dim clsLogEntry As New LogPLLEntry()
                clsLogEntry.LogMessage(exException.Message(), exException.Source, exException.StackTrace, exException.TargetSite, exException.GetType.ToString, strAppName, EventLogEntryType.Error, strBuilder, blnWeb)
                strErrMsg = strBuilder.ToString
                clsLogEntry = Nothing
                strBuilder = Nothing
                MessageBox.Show(strErrMsg, "frmSignon_Load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
            Finally
                Application.Exit()
            End Try
        Else
            Me.Show()
            StatusBar1.Text = "Validating user..."
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
            blnCheckUserName = CheckUserName()
            Cursor.Current = System.Windows.Forms.Cursors.Default
        End If

According to the AppActivate function, it doesn't take care of maximizing or minimizing a form. Is there any way for me to somehow maximize the form via code? I tried the "me.WindowState = FormWindowState.Maximized", but the problem is, is that I don't know how I can reference the form after I issue the Appactivate statement and before I send the keycode value in order to actually issue a command against it.
MigrationUser 1  Wednesday, February 26, 2003 2:37 PM
I added "me.WindowState = FormWindowState.Maximized" to the forms' Activated event which solved the problem.......
MigrationUser 1  Wednesday, February 26, 2003 4:28 PM

You can use google to search for other answers

Custom Search

More Threads

• Problem in Assign Date in MaskedtextBox!!!!
• How does Windows XP work when I update regitsries?
• Lost in a maze of upgrades and versions
• Active Directory Search
• C# saving and opening file for windows addressbook
• Control.OnKeyDown() - call base.OnKeyDown() before or after processing?
• Linking to a resource file within the WebBrowser object
• Datagridview cell with DataGridViewCellStyle set to date won't accept / slash
• How to paint on a form from another class?
• Scrolling a window with VScrollBar.