Reputation: 19
This is my code for simulate mouse automation on an On-screen keyboard.
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32")]
//[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int apimouse_event(Int32 dwFlags, Int32 dX, Int32 dY, Int32 cButtons, Int32 dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
// For Win7 Home Edition 64 bit, to open OSK, go to Project -> Application Properties -> Compile ->
// Target CPU -> set to x64 (in Visual Studio 2012)
// Note app must be run with admin privileges to move window.
[DllImport("user32.dll")]
private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
private Process OSK;
private Process ShutDownOSK;
private Dictionary<string, Point> KeyboardKeys = new Dictionary<string, Point>();
private Dictionary<string, Point> KeyboardNonKeys = new Dictionary<string, Point>();
private List<string> TestStringList = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
KeyboardKeys.Add("Escape", new Point(5, 34));
KeyboardKeys.Add("`", new Point(11, 34));
KeyboardKeys.Add("1", new Point(16, 34));
KeyboardKeys.Add("2", new Point(21, 33));
KeyboardKeys.Add("3", new Point(26, 33));
KeyboardKeys.Add("4", new Point(31, 34));
KeyboardKeys.Add("5", new Point(36, 33));
KeyboardKeys.Add("6", new Point(41, 34));
KeyboardKeys.Add("7", new Point(46, 33));
KeyboardKeys.Add("8", new Point(51, 33));
KeyboardKeys.Add("9", new Point(57, 34));
KeyboardKeys.Add("0", new Point(62, 33));
KeyboardKeys.Add("-", new Point(67, 33));
KeyboardKeys.Add("=", new Point(72, 34));
KeyboardKeys.Add("Back Space", new Point(78, 34));
KeyboardKeys.Add("Home", new Point(86, 34));
KeyboardKeys.Add("Page Up", new Point(94, 34));
KeyboardKeys.Add("Tab", new Point(6, 47));
KeyboardKeys.Add("q", new Point(14, 47));
KeyboardKeys.Add("w", new Point(19, 48));
KeyboardKeys.Add("e", new Point(24, 48));
KeyboardKeys.Add("r", new Point(29, 48));
KeyboardKeys.Add("t", new Point(34, 47));
KeyboardKeys.Add("y", new Point(39, 47));
KeyboardKeys.Add("u", new Point(44, 47));
KeyboardKeys.Add("i", new Point(49, 48));
KeyboardKeys.Add("o", new Point(54, 48));
KeyboardKeys.Add("p", new Point(59, 48));
KeyboardKeys.Add("[", new Point(64, 48));
KeyboardKeys.Add("]", new Point(70, 49));
KeyboardKeys.Add(@"\", new Point(74, 48));
KeyboardKeys.Add("Delete", new Point(79, 48));
KeyboardKeys.Add("End", new Point(86, 48));
KeyboardKeys.Add("Page Down", new Point(94, 48));
KeyboardKeys.Add("Caps Lock", new Point(7, 62));
KeyboardKeys.Add("a", new Point(16, 61));
KeyboardKeys.Add("s", new Point(21, 62));
KeyboardKeys.Add("d", new Point(26, 62));
KeyboardKeys.Add("f", new Point(31, 62));
KeyboardKeys.Add("g", new Point(36, 62));
KeyboardKeys.Add("h", new Point(42, 61));
KeyboardKeys.Add("j", new Point(46, 61));
KeyboardKeys.Add("k", new Point(52, 61));
KeyboardKeys.Add("l", new Point(57, 61));
KeyboardKeys.Add(";", new Point(62, 61));
KeyboardKeys.Add("'", new Point(67, 61));
KeyboardKeys.Add("Enter", new Point(75, 61));
KeyboardKeys.Add("Insert", new Point(86, 61));
KeyboardKeys.Add("Pause", new Point(95, 62));
KeyboardKeys.Add("Shift", new Point(8, 75));
KeyboardKeys.Add("z", new Point(19, 75));
KeyboardKeys.Add("x", new Point(24, 75));
KeyboardKeys.Add("c", new Point(29, 75));
KeyboardKeys.Add("v", new Point(34, 75));
KeyboardKeys.Add("b", new Point(39, 75));
KeyboardKeys.Add("n", new Point(44, 75));
KeyboardKeys.Add("m", new Point(49, 75));
KeyboardKeys.Add(",", new Point(54, 75));
KeyboardKeys.Add(".", new Point(59, 75));
KeyboardKeys.Add("/", new Point(64, 75));
KeyboardKeys.Add("Up Arrow", new Point(69, 75));
KeyboardKeys.Add("Right Shift", new Point(77, 75));
KeyboardKeys.Add("Print Screen", new Point(86, 75));
KeyboardKeys.Add("ScrLk", new Point(95, 75));
KeyboardKeys.Add("Control", new Point(5, 89));
KeyboardKeys.Add("Windows", new Point(11, 89));
KeyboardKeys.Add("Alternate", new Point(16, 90));
KeyboardKeys.Add("Space", new Point(30, 89));
KeyboardKeys.Add("Right Alternate", new Point(47, 90));
KeyboardKeys.Add("Right Control", new Point(58, 88));
KeyboardKeys.Add("Left Arrow", new Point(64, 88));
KeyboardKeys.Add("Down Arrow", new Point(69, 89));
KeyboardKeys.Add("Right Arrow", new Point(75, 89));
KeyboardKeys.Add("Options", new Point(87, 89));
KeyboardKeys.Add("Help", new Point(95, 88));
KeyboardKeys.Add("Fn", new Point(79, 89));
KeyboardKeys.Add("I have no idea", new Point(51, 90));
KeyboardNonKeys.Add("Control Box Minimize", new Point(97, 9));
KeyboardNonKeys.Add("X Close", new Point(31, 9));
KeyboardNonKeys.Add("System Menu", new Point(15, 14));
foreach (var Item in KeyboardKeys)
ListBox1.Items.Add(Item.Key.ToString());
foreach (var Item in KeyboardNonKeys)
ListBox1.Items.Add(Item.Key.ToString());
var CheckStatus = Process.GetProcessesByName("osk");
if (CheckStatus.Length > 0)
return;
else
{
Process.Start("osk");
System.Threading.Thread.Sleep(1000);
var OEaCB = Process.GetProcessesByName("osk");
OSK = OEaCB[0];
}
TestStringList.Add("Shift");
TestStringList.Add("h");
TestStringList.Add("e");
TestStringList.Add("l");
TestStringList.Add("l");
TestStringList.Add("o");
TestStringList.Add("Space");
TestStringList.Add("Shift");
TestStringList.Add("s");
TestStringList.Add("k");
TestStringList.Add("y");
TestStringList.Add("p");
TestStringList.Add("e");
TestStringList.Add("Space");
TestStringList.Add("Shift");
TestStringList.Add("d");
TestStringList.Add("u");
TestStringList.Add("d");
TestStringList.Add("e");
TestStringList.Add(".");
TestStringList.Add("Space");
TestStringList.Add("Shift");
TestStringList.Add("t");
TestStringList.Add("h");
TestStringList.Add("i");
TestStringList.Add("s");
TestStringList.Add("Space");
TestStringList.Add("w");
TestStringList.Add("a");
TestStringList.Add("s");
TestStringList.Add("Space");
TestStringList.Add("a");
TestStringList.Add("u");
TestStringList.Add("t");
TestStringList.Add("o");
TestStringList.Add("t");
TestStringList.Add("y");
TestStringList.Add("p");
TestStringList.Add("e");
TestStringList.Add("d");
TestStringList.Add("Space");
TestStringList.Add("b");
TestStringList.Add("y");
TestStringList.Add("Space");
TestStringList.Add("t");
TestStringList.Add("h");
TestStringList.Add("e");
TestStringList.Add("Space");
TestStringList.Add("t");
TestStringList.Add("i");
TestStringList.Add("m");
TestStringList.Add("e");
TestStringList.Add("r");
TestStringList.Add(".");
TestStringList.Add("Enter");
TestStringList.Add("Enter");
TestStringList.Add("Shift");
TestStringList.Add("c");
TestStringList.Add("Shift");
TestStringList.Add("o");
TestStringList.Add("Shift");
TestStringList.Add("o");
TestStringList.Add("Shift");
TestStringList.Add("l");
TestStringList.Add("Shift");
TestStringList.Add("1");
TestStringList.Add("Shift");
TestStringList.Add("1");
TestStringList.Add("Shift");
TestStringList.Add("1");
TestStringList.Add("Shift");
TestStringList.Add("1");
TestStringList.Add("Shift");
TestStringList.Add("1");
Timer1.Interval = 1000;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
var CheckStatus = Process.GetProcessesByName("osk");
if (CheckStatus.Length > 0)
{
ShutDownOSK = CheckStatus[0];
ShutDownOSK.Kill();
}
else
return;
}
private RECT rec;
private Rectangle NewRect = new Rectangle();
private void Button1_Click(object sender, EventArgs e)
{
GetWindowRect(OSK.MainWindowHandle, out rec);
NewRect.X = rec.left;
NewRect.Y = rec.top;
NewRect.Width = rec.right - rec.left + 1;
NewRect.Height = rec.bottom - rec.top + 1;
Label1.Text = "OSK width = " + NewRect.Width.ToString() + " .. OSK height = " + NewRect.Height.ToString();
}
private int Counter = 0;
private void Timer1_Tick(object sender, EventArgs e)
{
Timer1.Interval = 50;
Point ClickPoint = KeyboardKeys[TestStringList[Counter]];
int PosX = System.Convert.ToInt32(NewRect.Left + ((NewRect.Right - NewRect.Left) / (double)100 * System.Convert.ToInt32(ClickPoint.X)));
int PosY = System.Convert.ToInt32(NewRect.Top + ((NewRect.Bottom - NewRect.Top) / (double)100 * System.Convert.ToInt32(ClickPoint.Y)));
Cursor.Position = new Point(PosX, PosY);
RichTextBox1.Focus();
apimouse_event(MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, PosX, PosY, 0, 0);
Counter += 1;
if (Counter == TestStringList.Count - 1)
{
Timer1.Stop();
Counter = 0;
}
}
private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedItem.ToString() != "Control Box Minimize" | ListBox1.SelectedItem.ToString() != "X Close" | ListBox1.SelectedItem.ToString() != "System Menu")
{
Point ClickPoint = KeyboardKeys[ListBox1.SelectedItem.ToString()];
Text = ClickPoint.ToString();
int PosX = System.Convert.ToInt32(NewRect.Left + ((NewRect.Right - NewRect.Left) / (double)100 * System.Convert.ToInt32(ClickPoint.X)));
int PosY = System.Convert.ToInt32(NewRect.Top + ((NewRect.Bottom - NewRect.Top) / (double)100 * System.Convert.ToInt32(ClickPoint.Y)));
Cursor.Position = new Point(PosX, PosY);
RichTextBox1.Focus();
apimouse_event(MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, PosX, PosY, 0, 0);
}
}
private void Button2_Click(object sender, EventArgs e)
{
Timer1.Start();
}
}
But I'm getting an error when I call it:
apimouse_event(MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, PosX, PosY, 0, 0);
Error:
An unhandled exception of type 'System.EntryPointNotFoundException' occurred in onkeyboard.exe
Additional information: Unable to find an entry point named 'apimouse_event' in DLL 'user32'.
Upvotes: 0
Views: 337
Reputation: 145
The error looks like there is no apimouse_event
function in the user32
library. Make sure you written the correct name of both the library and the function.
Upvotes: 0