public static Keys IsAltPrefix(ref string s) { if (ObjectExtensionsStrings.IsPrefix(ref s, "Alt", StringComparison.InvariantCultureIgnoreCase)) { return(Keys.Menu); } else if (ObjectExtensionsStrings.IsPrefix(ref s, "RAlt", StringComparison.InvariantCultureIgnoreCase)) { return(Keys.RMenu); } else { return(Keys.None); } }
public static Keys IsCtrlPrefix(ref string s) { if (ObjectExtensionsStrings.IsPrefix(ref s, "Ctrl", StringComparison.InvariantCultureIgnoreCase)) { return(Keys.ControlKey); } else if (ObjectExtensionsStrings.IsPrefix(ref s, "RCtrl", StringComparison.InvariantCultureIgnoreCase)) { return(Keys.RControlKey); } else { return(Keys.None); } }
public static Keys IsShiftPrefix(ref string s) // look for prefix, remove, return what the prefix is { if (ObjectExtensionsStrings.IsPrefix(ref s, "Shift", StringComparison.InvariantCultureIgnoreCase)) { return(Keys.ShiftKey); } else if (ObjectExtensionsStrings.IsPrefix(ref s, "RShift", StringComparison.InvariantCultureIgnoreCase)) { return(Keys.RShiftKey); } else { return(Keys.None); } }
public static string ParseKeys(Queue <SKEvent> events, string s, int defdelay, int defshiftdelay, int defupdelay, IAdditionalKeyParser additionalkeyparser = null) { //debugevents = null; s = s.Trim(); IntPtr hwnd = (IntPtr)0; while (s.Length > 0) { if (additionalkeyparser != null) // See if key needs translating out - moved to here to allow for control sequences before this key { Tuple <string, int, string> t = additionalkeyparser.Parse(s); // Allow the parser to sniff the string if (t.Item3 != null) // error condition here, such as no matching key binding { return(t.Item3); } if (t.Item1 != null) // if replace.. (and the parser can return multiple keys) { s = t.Item1 + " " + s.Substring(t.Item2); // its the replace string, followed by the cut out current string } } int d1 = -1, d2 = -1, d3 = -1; if (s.Length >= 1 && s[0] == '[') { if (!s.Contains(']')) { return("Missing closing ] in delay"); } s = s.Substring(1); string word = ObjectExtensionsStrings.FirstWord(ref s, new char[] { ']', ',' }); if (!word.InvariantParse(out d1)) { return("Delay not properly given"); } if (s.Length >= 1 && s[0] == ',') { s = s.Substring(1); word = ObjectExtensionsStrings.FirstWord(ref s, new char[] { ']', ',' }); if (!word.InvariantParse(out d2)) { return("Second Delay not properly given"); } } if (s.Length >= 1 && s[0] == ',') { s = s.Substring(1); word = ObjectExtensionsStrings.FirstWord(ref s, new char[] { ']' }); if (!word.InvariantParse(out d3)) { return("Third Delay not properly given"); } } if (s.Length >= 1 && s[0] == ']') { s = s.Substring(1); } else { return("Missing closing ] in delay"); } } KMode kmd = KMode.press; if (s.Length == 0) { return("Invalid no characters after delay"); } if (s[0] == '^' || s[0] == '<') { kmd = KMode.up; s = s.Substring(1); } else if (s[0] == '!' || s[0] == '>') { kmd = KMode.down; s = s.Substring(1); } if (additionalkeyparser != null) // Also see here if key needs translating out - 9.0.3.0 { Tuple <string, int, string> t = additionalkeyparser.Parse(s); // Allow the parser to sniff the string if (t.Item3 != null) // error condition here, such as no matching key binding { return(t.Item3); } if (t.Item1 != null) // if replace.. (and the parser can return multiple keys) { s = t.Item1 + " " + s.Substring(t.Item2); // its the replace string, followed by the cut out current string } } Keys shift = KeyObjectExtensions.IsShiftPrefix(ref s); Keys ctrl = Keys.None; Keys alt = Keys.None; if (shift == Keys.None || s.StartsWith("+")) { s = s.Skip("+"); alt = KeyObjectExtensions.IsAltPrefix(ref s); if (alt == Keys.None || s.StartsWith("+")) { s = s.Skip("+"); ctrl = KeyObjectExtensions.IsCtrlPrefix(ref s); if (ctrl != Keys.None) { s = s.Skip("+"); } } } bool mainpart = s.Length > 0 && s[0] != ' '; // keydown is d1 or def int keydowndelay = (d1 != -1) ? d1 : defdelay; // if mainpart present, its d2 or defshift. If no main part, its d1 or def shift int shiftdelay = (mainpart) ? (d2 != -1 ? d2 : defshiftdelay) : (d1 != -1 ? d1 : defshiftdelay); // if in up/down mode, its d1 or def up. If its got a main part, its d3/defup. else its d2/defup int keyupdelay = (kmd == KMode.up || kmd == KMode.down) ? (d1 != -1 ? d1 : defupdelay) : (mainpart ? (d3 != -1 ? d3 : defupdelay) : (d2 != -1 ? d2 : defupdelay)); //System.Diagnostics.Debug.WriteLine(string.Format("{0} {1} {2} {3} {4} {5} ", d1, d2, d3, keydowndelay, shiftdelay, keyupdelay)); if (shift != Keys.None) // we already run shift keys here. If we are doing UP, we send a up, else we are doing down/press { events.Enqueue(new SKEvent(kmd == KMode.up ? BaseUtils.Win32Constants.WM.KEYUP : BaseUtils.Win32Constants.WM.KEYDOWN, shift, shiftdelay)); } if (ctrl != Keys.None) { events.Enqueue(new SKEvent(kmd == KMode.up ? BaseUtils.Win32Constants.WM.KEYUP : BaseUtils.Win32Constants.WM.KEYDOWN, ctrl, shiftdelay)); } if (alt != Keys.None) { events.Enqueue(new SKEvent(kmd == KMode.up ? BaseUtils.Win32Constants.WM.SYSKEYUP : BaseUtils.Win32Constants.WM.SYSKEYDOWN, alt, shiftdelay)); } if (mainpart) { if (s.Length == 0) { return("Invalid no characters after shifters"); } bool brackets = ObjectExtensionsStrings.IsPrefix(ref s, "("); while (s.Length > 0) { string word = ObjectExtensionsStrings.FirstWord(ref s, new char[] { ' ', ')' }); Keys key = word.ToVkey(); if (key != Keys.None) { AddMsgsForVK(events, key, alt != Keys.None && ctrl == Keys.None, keydowndelay, keyupdelay, kmd); //System.Diagnostics.Debug.WriteLine(shift + " " + alt + " " + ctrl + " press " + key.VKeyToString()); } else { while (word.Length > 0) { string ch = new string(word[0], 1); key = ch.ToVkey(); if (key.IsSingleCharName()) { AddMsgsForVK(events, key, alt != Keys.None && ctrl == Keys.None, keydowndelay, keyupdelay, kmd); //System.Diagnostics.Debug.WriteLine(shift + " " + alt + " " + ctrl + " press " + key.VKeyToString()); word = word.Substring(1); } else { return("Invalid key " + word); } } } if (!brackets) { break; } else if (s.Length > 0 && s[0] == ')') { s = s.Substring(1); break; } } } if (kmd == KMode.press) // only on a press do we release here { if (alt != Keys.None) { events.Enqueue(new SKEvent(BaseUtils.Win32Constants.WM.SYSKEYUP, alt, keyupdelay)); } if (ctrl != Keys.None) { events.Enqueue(new SKEvent(BaseUtils.Win32Constants.WM.KEYUP, ctrl, keyupdelay)); } if (shift != Keys.None) { events.Enqueue(new SKEvent(BaseUtils.Win32Constants.WM.KEYUP, shift, keyupdelay)); } } s = s.Trim(); if (s.Length > 0 && s[0] == ',') // comma can be used between key groups { s = s.Substring(1).TrimStart(); } } return(""); }
public static string ParseKeys(string s, int defdelay, int defshiftdelay, int defupdelay) { //debugevents = null; s = s.Trim(); IntPtr hwnd = (IntPtr)0; while (s.Length > 0) { KMode kmd = KMode.press; int d1 = -1, d2 = -1, d3 = -1; if (s[0] == '[') { s = s.Substring(1); string word = ObjectExtensionsStrings.FirstWord(ref s, new char[] { ']', ',' }); if (!word.InvariantParse(out d1)) { return("Delay not properly given"); } if (s[0] == ',') { s = s.Substring(1); word = ObjectExtensionsStrings.FirstWord(ref s, new char[] { ']', ',' }); if (!word.InvariantParse(out d2)) { return("Second Delay not properly given"); } } if (s[0] == ',') { s = s.Substring(1); word = ObjectExtensionsStrings.FirstWord(ref s, new char[] { ']' }); if (!word.InvariantParse(out d3)) { return("Third Delay not properly given"); } } if (s[0] == ']') { s = s.Substring(1); } else { return("Missing closing ] in delay"); } } if (s[0] == '^' || s[0] == '<') { kmd = KMode.up; s = s.Substring(1); } else if (s[0] == '!' || s[0] == '>') { kmd = KMode.down; s = s.Substring(1); } Keys shift = KeyObjectExtensions.IsShiftPrefix(ref s); Keys ctrl = Keys.None; Keys alt = Keys.None; if (shift == Keys.None || s.StartsWith("+")) { s = s.Skip("+"); alt = KeyObjectExtensions.IsAltPrefix(ref s); if (alt == Keys.None || s.StartsWith("+")) { s = s.Skip("+"); ctrl = KeyObjectExtensions.IsCtrlPrefix(ref s); if (ctrl != Keys.None) { s = s.Skip("+"); } } } bool mainpart = s.Length > 0 && s[0] != ' '; // keydown is d1 or def int keydowndelay = (d1 != -1) ? d1 : defdelay; // if mainpart present, its d2 or defshift. If no main part, its d1 or def shift int shiftdelay = (mainpart) ? (d2 != -1 ? d2 : defshiftdelay) : (d1 != -1 ? d1 : defshiftdelay); // if in up/down mode, its d1 or def up. If its got a main part, its d3/defup. else its d2/defup int keyupdelay = (kmd == KMode.up || kmd == KMode.down) ? (d1 != -1 ? d1 : defupdelay) : (mainpart ? (d3 != -1 ? d3: defupdelay) : (d2 != -1 ? d2 : defupdelay)); System.Diagnostics.Debug.WriteLine(string.Format("{0} {1} {2} {3} {4} {5} ", d1, d2, d3, keydowndelay, shiftdelay, keyupdelay)); if (shift != Keys.None) // we already run shift keys here. If we are doing UP, we send a up, else we are doing down/press { AddEvent(new SKEvent(kmd == KMode.up ? BaseUtils.Win32Constants.WM.KEYUP : BaseUtils.Win32Constants.WM.KEYDOWN, shift, shiftdelay)); } if (ctrl != Keys.None) { AddEvent(new SKEvent(kmd == KMode.up ? BaseUtils.Win32Constants.WM.KEYUP : BaseUtils.Win32Constants.WM.KEYDOWN, ctrl, shiftdelay)); } if (alt != Keys.None) { AddEvent(new SKEvent(kmd == KMode.up ? BaseUtils.Win32Constants.WM.SYSKEYUP: BaseUtils.Win32Constants.WM.SYSKEYDOWN, alt, shiftdelay)); } if (mainpart) { if (s.Length == 0) { return("Invalid no characters after shifters"); } bool brackets = ObjectExtensionsStrings.IsPrefix(ref s, "("); while (s.Length > 0) { string word = ObjectExtensionsStrings.FirstWord(ref s, new char[] { ' ', ')' }); Keys key = word.ToVkey(); if (key != Keys.None) { AddMsgsForVK(key, alt != Keys.None && ctrl == Keys.None, keydowndelay, keyupdelay, kmd); //System.Diagnostics.Debug.WriteLine(shift + " " + alt + " " + ctrl + " press " + key.VKeyToString()); } else { while (word.Length > 0) { string ch = new string(word[0], 1); key = ch.ToVkey(); if (key.IsSingleCharName()) { AddMsgsForVK(key, alt != Keys.None && ctrl == Keys.None, keydowndelay, keyupdelay, kmd); //System.Diagnostics.Debug.WriteLine(shift + " " + alt + " " + ctrl + " press " + key.VKeyToString()); word = word.Substring(1); } else { return("Invalid key " + word); } } } if (!brackets) { break; } else if (s.Length > 0 && s[0] == ')') { s = s.Substring(1); break; } } } if (kmd == KMode.press) // only on a press do we release here { if (alt != Keys.None) { AddEvent(new SKEvent(BaseUtils.Win32Constants.WM.SYSKEYUP, alt, keyupdelay)); } if (ctrl != Keys.None) { AddEvent(new SKEvent(BaseUtils.Win32Constants.WM.KEYUP, ctrl, keyupdelay)); } if (shift != Keys.None) { AddEvent(new SKEvent(BaseUtils.Win32Constants.WM.KEYUP, shift, keyupdelay)); } } s = s.Trim(); if (s.Length > 0 && s[0] == ',') // comma can be used between key groups { s = s.Substring(1).TrimStart(); } } return(""); }