Skip to content

Commit b8b6a87

Browse files
Added key_shortcut
1 parent 4ca2487 commit b8b6a87

File tree

2 files changed

+125
-30
lines changed

2 files changed

+125
-30
lines changed

AssistantComputerControl/Actions.cs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,59 @@ class Actions {
4949
[DllImport("user32.dll", SetLastError = true)]
5050
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
5151

52+
//Key Shortcut
53+
/*
54+
* Keys and how to convert
55+
* Each one on the left is accepted as a key inside the paramater
56+
*/
57+
String[,] charactersType = new String[,] {
58+
{ "BACKSPACE", "{BACKSPACE}"},
59+
{ "BREAK", "{BREAK}"},
60+
{ "CAPS_LOCK", "{CAPSLOCK}"},
61+
{ "DEL", "{DELETE}"},
62+
{ "DELETE", "{DELETE}"},
63+
{ "DOWN_ARROW", "{DOWN}"},
64+
{ "END", "{END}"},
65+
{ "ENTER", "{ENTER}"},
66+
{ "ESC", "{ESC}"},
67+
{ "HELP", "{HELP}"},
68+
{ "HOME", "{HOME}"},
69+
{ "INS", "{INS}"},
70+
{ "INSERT", "{INSERT}"},
71+
{ "LEFT_ARROW", "{LEFT}"},
72+
{ "NUM_LOCK", "{NUMLOCK}"},
73+
{ "PAGE_DOWN", "{PGDN}"},
74+
{ "PAGE_UP", "{PGUP}"},
75+
{ "PRINT_SCREEN", "{PRTSC}"},
76+
{ "RIGHT_ARROW", "{RIGHT}"},
77+
{ "SCROLL_LOCK", "{SCROLLLOCK}"},
78+
{ "TAB", "{TAB}"},
79+
{ "UP_ARROW", "{UP}"},
80+
{ "F1", "{F1}"},
81+
{ "F2", "{F2}"},
82+
{ "F3", "{F3}"},
83+
{ "F4", "{F4}"},
84+
{ "F5", "{F5}"},
85+
{ "F6", "{F6}"},
86+
{ "F7", "{F7}"},
87+
{ "F8", "{F8}"},
88+
{ "F9", "{F9}"},
89+
{ "F10", "{F10}"},
90+
{ "F11", "{F11}"},
91+
{ "F12", "{F12}"},
92+
{ "F13", "{F13}"},
93+
{ "F14", "{F14}"},
94+
{ "F15", "{F15}"},
95+
{ "F16", "{F16}"},
96+
{ "Keypad_add", "{ADD}"},
97+
{ "Keypad_subtract", "{SUBTRACT}"},
98+
{ "Keypad_multiply", "{MULTIPLY}"},
99+
{ "Keypad_divide", "{DIVIDE}"},
100+
{ "SHIFT", "+"},
101+
{ "CTRL", "^"},
102+
{ "ALT", "%"}
103+
};
104+
52105
//Baisc things
53106
private void Error(string errorMsg, string debugMsg = null) {
54107
MainProgram.DoDebug("ERROR: " + (String.IsNullOrEmpty(debugMsg) ? errorMsg : debugMsg));
@@ -422,6 +475,75 @@ public void WriteOut(string parameter, string line) {
422475
successMessage = "Simulated writing \"" + writtenString + "\"";
423476
}
424477
}
478+
479+
public void KeyShortcut(string parameter) {
480+
/*
481+
* Added by : Joshua Miller
482+
* How to use it :
483+
* - To seperate keys please us '+' (to use '+' do {ADD})
484+
* - Things like ctrl will be converted to control key
485+
*/
486+
487+
// Split up commands
488+
char splitChar = '+';
489+
String[] keyCombinationInput = parameter.Split(splitChar);
490+
// Will be added onto to make what to type
491+
String keyCombinationPress = "";
492+
// Put commands into correct form
493+
for (int index = 0; index < keyCombinationInput.Length; index++) {
494+
// Get current command
495+
String command = keyCombinationInput[index];
496+
// If not empty
497+
if (command != "") {
498+
// If one character (not command)
499+
if (command.Length == 1) {
500+
// Add to the out
501+
keyCombinationPress = keyCombinationPress + command.ToLower();
502+
}
503+
else {
504+
// If it is a command (probably)
505+
// Check if it is a possible command and needs to be changed
506+
bool foundYet = false;
507+
for (int countInCharacterArray = 0; countInCharacterArray < charactersType.GetLength(0) && foundYet == false; countInCharacterArray++) {
508+
String characterTestNow = charactersType[countInCharacterArray, 0];
509+
if (Equals(command.ToUpper(), characterTestNow)) {
510+
keyCombinationPress += charactersType[countInCharacterArray, 1];
511+
foundYet = true;
512+
} else if (Equals(command.ToUpper(), charactersType[countInCharacterArray, 1])) {
513+
keyCombinationPress += charactersType[countInCharacterArray, 1];
514+
foundYet = true;
515+
}
516+
}
517+
if (foundYet == false) {
518+
MainProgram.DoDebug("KeyShortcut Action - Warning: A command " + command.ToUpper() + " was not identified, please be weary as this may not work");
519+
MainProgram.DoDebug("KeyShortcut Action - Warning: Adding Anyway");
520+
keyCombinationPress += command;
521+
}
522+
}
523+
} else {
524+
MainProgram.DoDebug("KeyShortcut Action - Warning: A character inside the paramater was blank");
525+
}
526+
}
527+
528+
// Is it testing?
529+
if (MainProgram.testingAction) {
530+
successMessage = ("Simulated sending the combination: " + keyCombinationPress);
531+
}
532+
else {
533+
// Try pressing keys
534+
bool keysPressedSuccess = true;
535+
try {
536+
SendKeys.SendWait(keyCombinationPress);
537+
} catch (ArgumentException) {
538+
Error("Key combination is not valid");
539+
keysPressedSuccess = false;
540+
}
541+
if (keysPressedSuccess) {
542+
successMessage = ("Sending the combination: " + keyCombinationPress);
543+
}
544+
}
545+
}
546+
425547
public void CreateFile(string parameter) {
426548
string fileLocation = parameter;
427549
if (!File.Exists(fileLocation)) {

AssistantComputerControl/actionChecker.cs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -315,43 +315,16 @@ public static void ExecuteAction(string action, string line, string parameter, s
315315
case "monitors_off":
316316
actionExecution.MonitorsOff(parameter);
317317
break;
318-
/*case "keypress":
318+
case "key_shortcut":
319319
if (RequireParameter(parameter)) {
320-
if (parameter.Length > 1) {
321-
if (!MainProgram.testingAction) {
322-
successMessage = "Pressed \"" + parameter + "\"";
323-
PressKey((char)parameter[0]);
324-
} else {
325-
successMessage = "Simulated press of \"" + parameter + "\"";
326-
}
327-
} else {
328-
MainProgram.DoDebug("ERROR: Parameter can only be one character long");
329-
MainProgram.errorMessage = "(Keypress) Parameter can only be one character long";
330-
}
320+
actionExecution.KeyShortcut(parameter);
331321
}
332-
break;*/
322+
break;
333323
case "write_out":
334324
if (RequireParameter(parameter)) {
335325
actionExecution.WriteOut(parameter, line);
336326
}
337327
break;
338-
/*case "key_shortcut": //TODO - version 1.3(?)
339-
//Currently just keeps holding CTRL down, fucking everything up
340-
//Do "testing" check
341-
if (RequireParameter(parameter)) {
342-
parameter = parameter.Replace("ctrl", "%");
343-
344-
/*foreach (char c in parameter) {
345-
if (c == '%') {
346-
keybd_event(VK_RCONTROL, 0, KEYEVENTF_EXTENTEDKEY, 0);
347-
keybd_event(VK_RCONTROL, 0, KEYEVENTF_KEYUP, 0);
348-
} else {
349-
PressKey(c);
350-
}
351-
}*/
352-
/*}
353-
PressKeys();
354-
break;*/
355328
case "create_file":
356329
if (RequireParameter(parameter)) {
357330
actionExecution.CreateFile(parameter);

0 commit comments

Comments
 (0)