Send keystrokes to an application
You can send key stokes virtually to other applications with this:
This code will send F11 key press to Mozilla Firefox, which will make it ‘fullscreen’.
int iHandle = FindWindow(null, "Mozilla Firefox");
SetForegroundWindow(iHandle);
SendKeys.Send("{F11}");
FindWindow and SetForegroundWindow are APIs, which you can access with
[DllImport("user32.dll")]
public static extern int FindWindow(
string lpClassName, // class name
string lpWindowName // window name
);
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(
int hWnd // handle to window
);
Full cs code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern int FindWindow(
string lpClassName, // class name
string lpWindowName // window name
);
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(
int hWnd // handle to window
);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int iHandle = FindWindow(null, “Mozilla Firefox”);SetForegroundWindow(iHandle);SendKeys.Send(”A”);
}
}
}
Related posts:






















