Skip to content

Commit a223cd8

Browse files
authored
Merge pull request #75 from CodingPupper3033/moveMouse
Added new actions
2 parents 9acf041 + e109b7d commit a223cd8

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

AssistantComputerControl/Actions.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,87 @@ public void WindowsToast() {
908908
//Todo - quite a bit of work
909909
}
910910

911+
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
912+
private static extern bool SetCursorPos(int x, int y);
913+
public void MoveMouse(String parameter) {
914+
parameter = parameter.Trim(new char[] { '(',')',' '});
915+
String[] parameterSplit = parameter.Split(',');
916+
917+
// Error Checking
918+
// If there is the correct amount of arguments
919+
if (parameterSplit.Length != 2) {
920+
Error("The parameter is either formatted incorrectly or does not have 2 values");
921+
// If param 1 is a number
922+
} else if ((Int32.TryParse(parameterSplit[0], out int param1))) {
923+
if ((Int32.TryParse(parameterSplit[1], out int param2))) {
924+
SetCursorPos(Int32.Parse(parameterSplit[0]), Int32.Parse(parameterSplit[1]));
925+
successMessage = "Moved Mouse to (" + (Int32.Parse(parameterSplit[0]).ToString() + ", " + Int32.Parse(parameterSplit[1])).ToString() + ")";
926+
} else {
927+
Error("Parameter 2 is not a number");
928+
}
929+
930+
// It isn't a number
931+
} else {
932+
Error("Parameter 1 is not a number");
933+
}
934+
}
935+
936+
[DllImport("user32.dll", EntryPoint = "mouse_event")]
937+
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
938+
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
939+
public const int MOUSEEVENTF_LEFTUP = 0x04;
940+
public void MouseLeftClick(string parameter) {
941+
// Try to get amount of times to click
942+
if (Int32.TryParse(parameter, out int repeatAmount)) {
943+
for (int count = 0; count < repeatAmount; count++) {
944+
mouse_event(MOUSEEVENTF_LEFTDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0);
945+
mouse_event(MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
946+
successMessage = "Simulated pressing the Left mouse button " + repeatAmount + " times";
947+
}
948+
} else {
949+
Error("Repeat amount is not a munber");
950+
}
951+
}
952+
953+
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
954+
public const int MOUSEEVENTF_RIGHTUP = 0x10;
955+
public void MouseRightClick(string parameter) {
956+
// Try to get amount of times to click
957+
if (Int32.TryParse(parameter, out int repeatAmount)) {
958+
for (int count = 0; count < repeatAmount; count++) {
959+
mouse_event(MOUSEEVENTF_RIGHTDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0);
960+
mouse_event(MOUSEEVENTF_RIGHTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
961+
successMessage = "Simulated pressing the Right mouse button " + repeatAmount + " times";
962+
}
963+
} else {
964+
Error("Repeat amount is not a munber");
965+
}
966+
}
967+
968+
public const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
969+
public const int MOUSEEVENTF_MIDDLEUP = 0x40;
970+
public void MouseMiddleClick(string parameter) {
971+
// Try to get amount of times to click
972+
if (Int32.TryParse(parameter, out int repeatAmount)) {
973+
for (int count = 0; count < repeatAmount; count++) {
974+
mouse_event(MOUSEEVENTF_MIDDLEDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0);
975+
mouse_event(MOUSEEVENTF_MIDDLEUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
976+
successMessage = "Simulated pressing the Middle mouse button " + repeatAmount + " times";
977+
}
978+
} else {
979+
Error("Repeat amount is not a munber");
980+
}
981+
}
982+
983+
public void Wait(string parameter) {
984+
if (Int32.TryParse(parameter, out int time)) {
985+
Thread.Sleep(time);
986+
successMessage = "Waited " + time + " miliseconds";
987+
} else {
988+
Error("Time Parameter is not a number");
989+
}
990+
}
991+
911992
/* End of actions */
912993
}
913994
}

AssistantComputerControl/actionChecker.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,37 @@ public static Actions ExecuteAction(string action, string line, string parameter
517517
case "maximize_all":
518518
actionExecution.MaximizeAll();
519519
break;
520+
case "move_mouse":
521+
if (RequireParameter(parameter)) {
522+
actionExecution.MoveMouse(parameter);
523+
}
524+
break;
525+
case "mouse_left_click":
526+
if (String.IsNullOrEmpty(parameter)) {
527+
actionExecution.MouseLeftClick("1");
528+
} else {
529+
actionExecution.MouseLeftClick(parameter);
530+
}
531+
break;
532+
case "mouse_right_click":
533+
if (String.IsNullOrEmpty(parameter)) {
534+
actionExecution.MouseRightClick("1");
535+
} else {
536+
actionExecution.MouseRightClick(parameter);
537+
}
538+
break;
539+
case "mouse_middle_click":
540+
if (String.IsNullOrEmpty(parameter)) {
541+
actionExecution.MouseMiddleClick("1");
542+
} else {
543+
actionExecution.MouseMiddleClick(parameter);
544+
}
545+
break;
546+
case "wait":
547+
if (RequireParameter(parameter)) {
548+
actionExecution.Wait(parameter);
549+
}
550+
break;
520551
default:
521552
//Unknown action
522553
actionExecution.errorMessage = "Unknown action \"" + action + "\"";

0 commit comments

Comments
 (0)