Dominating
Dominating

Reputation: 2930

Simulating Keys C# .Net

I am working on VS 2010 Ultimate. I have created a simple console application about 25-30 rows. So I want in the Main() function simply to simulate pressing "ALT+TAB" in a while cycle. Hoh can I do that - I cant use SendKeys class cause it "Provides methods for sending keystrokes to an application." I want just when I start my console application to simulate 1000 times pressing "ALT+TAB" without attaching it to anny applications. Something like this:

using System;
using System.Windows.Forms;

namespace nagradite
{
    class Program
    {
        static void Main()
        {
            int i = 1000;
            while( i > 0 )
            {
              // PRESS "ALT+TAB"
                i--;
            }            
        }
    }
}

what should I type instead of // PRESS "ALT+TAB"

Upvotes: 2

Views: 1871

Answers (3)

Javed Akram
Javed Akram

Reputation: 15344

Use "%{TAB}" for Alt+TAB

SendKeys.Send("%{TAB}");

SendKeys.Send("%{TAB} 1000"); //if you want to do same by 1000 times as you stated

Reference http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

Upvotes: 2

Saeed Amiri
Saeed Amiri

Reputation: 22555

I think you can use SendMessage for finding keys you can see http://www.blizzhackers.cc/viewtopic.php?t=396398, your parent window is null (desktop)

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

Upvotes: 1

Josh
Josh

Reputation: 69252

You can try a Win32 API called SendInput. It allows you to simulate keyboard/mouse input events and does not require a HWND target. However, I don't know if this will actually trigger system-wide keyboard shortcuts such as ALT+TAB.

MSDN - http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx
PInvoke.Net - http://www.pinvoke.net/default.aspx/user32.sendinput

Upvotes: 1

Related Questions