Example #1
0
        private static void Beep(SiEvent si)
        {
            try
            {
                string str = si.Text;
                if (string.IsNullOrEmpty(str))
                {
                    SystemSounds.Beep.Play();
                    return;
                }

                string[] v = str.Split(new char[] { ' ', '\t' },
                                       StringSplitOptions.RemoveEmptyEntries);

                int f = 800, d = 200;                 // Defaults of Console.Beep()
                if (v.Length >= 1)
                {
                    int.TryParse(v[0], out f);
                }
                if (v.Length >= 2)
                {
                    int.TryParse(v[1], out d);
                }

                f = Math.Min(Math.Max(f, 37), 32767);
                if (d <= 0)
                {
                    return;
                }

                Console.Beep(f, d);                 // Throws on a server
            }
            catch (Exception) { Debug.Assert(false); }
        }
Example #2
0
        private static void FixEventSeq(List <SiEvent> l)
        {
            // Convert chars to keys
            // Keys kMod = Keys.None;
            for (int i = 0; i < l.Count; ++i)
            {
                SiEvent     si = l[i];
                SiEventType t  = si.Type;

                // if(t == SiEventType.KeyModifier)
                // {
                //	if(!si.Down.HasValue) { Debug.Assert(false); continue; }
                //	if(si.Down.Value)
                //	{
                //		Debug.Assert((kMod & si.KeyModifier) == Keys.None);
                //		kMod |= si.KeyModifier;
                //	}
                //	else
                //	{
                //		Debug.Assert((kMod & si.KeyModifier) == si.KeyModifier);
                //		kMod &= ~si.KeyModifier;
                //	}
                // }
                if (t == SiEventType.Char)
                {
                    // bool bLightConv = (kMod == Keys.None);
                    int iVKey = SiCodes.CharToVKey(si.Char, true);
                    if (iVKey > 0)
                    {
                        si.Type = SiEventType.Key;
                        si.VKey = iVKey;
                    }
                }
            }
        }
Example #3
0
 private static bool IsClipboardOp(SiEvent si)
 {
     if (si == null)
     {
         Debug.Assert(false); return(false);
     }
     return(si.Type == SiEventType.ClipboardCopy);
 }
Example #4
0
        private static void AppActivate(SiEvent si)
        {
            try
            {
                if (string.IsNullOrEmpty(si.Text))
                {
                    return;
                }

                IntPtr h = NativeMethods.FindWindow(si.Text);
                if (h != IntPtr.Zero)
                {
                    NativeMethods.EnsureForegroundWindow(h);
                }
            }
            catch (Exception) { Debug.Assert(false); }
        }
Example #5
0
        private static void EnsureKeyModifiers(Keys kReqMods, ref Keys kCurKbMods,
                                               List <SiEvent> l)
        {
            if (kReqMods == kCurKbMods)
            {
                return;
            }

            if ((kReqMods & Keys.Shift) != (kCurKbMods & Keys.Shift))
            {
                SiEvent si = new SiEvent();
                si.Type        = SiEventType.KeyModifier;
                si.KeyModifier = Keys.Shift;
                si.Down        = ((kReqMods & Keys.Shift) != Keys.None);

                l.Add(si);
            }

            if ((kReqMods & Keys.Control) != (kCurKbMods & Keys.Control))
            {
                SiEvent si = new SiEvent();
                si.Type        = SiEventType.KeyModifier;
                si.KeyModifier = Keys.Control;
                si.Down        = ((kReqMods & Keys.Control) != Keys.None);

                l.Add(si);
            }

            if ((kReqMods & Keys.Alt) != (kCurKbMods & Keys.Alt))
            {
                SiEvent si = new SiEvent();
                si.Type        = SiEventType.KeyModifier;
                si.KeyModifier = Keys.Alt;
                si.Down        = ((kReqMods & Keys.Alt) != Keys.None);

                l.Add(si);
            }

            kCurKbMods = kReqMods;
        }
Example #6
0
        private static List <SiEvent> ParseSpecial(CharStream cs)
        {
            // Skip leading white space
            while (true)
            {
                char ch = cs.PeekChar();
                if (ch == char.MinValue)
                {
                    Debug.Assert(false); return(null);
                }

                if (!char.IsWhiteSpace(ch))
                {
                    break;
                }
                cs.ReadChar();
            }

            // First char is *always* part of the name (support for "{{}", etc.)
            char chFirst = cs.ReadChar();

            if (chFirst == char.MinValue)
            {
                Debug.Assert(false); return(null);
            }

            int           iPart = 0;
            StringBuilder sbName = new StringBuilder(), sbParams =
                new StringBuilder();

            sbName.Append(chFirst);

            while (true)
            {
                char ch = cs.ReadChar();
                if (ch == char.MinValue)
                {
                    Debug.Assert(false); return(null);
                }
                if (ch == '}')
                {
                    break;
                }

                if (iPart == 0)
                {
                    if (char.IsWhiteSpace(ch))
                    {
                        ++iPart;
                    }
                    else
                    {
                        sbName.Append(ch);
                    }
                }
                else
                {
                    sbParams.Append(ch);
                }
            }

            string strName   = sbName.ToString();
            string strParams = sbParams.ToString().Trim();

            uint?ouParam = null;

            if (strParams.Length > 0)
            {
                uint uParamTry;
                if (uint.TryParse(strParams, out uParamTry))
                {
                    ouParam = uParamTry;
                }
            }

            List <SiEvent> l = new List <SiEvent>();

            if (strName.Equals("DELAY", StrUtil.CaseIgnoreCmp))
            {
                if (!ouParam.HasValue)
                {
                    Debug.Assert(false); return(null);
                }

                SiEvent si = new SiEvent();
                si.Type  = SiEventType.Delay;
                si.Delay = ouParam.Value;

                l.Add(si);
                return(l);
            }
            if (strName.StartsWith("DELAY=", StrUtil.CaseIgnoreCmp))
            {
                SiEvent si = new SiEvent();
                si.Type = SiEventType.SetDefaultDelay;

                string strDelay = strName.Substring(6).Trim();
                uint   uDelay;
                if (uint.TryParse(strDelay, out uDelay))
                {
                    si.Delay = uDelay;
                }
                else
                {
                    Debug.Assert(false); return(null);
                }

                l.Add(si);
                return(l);
            }
            if (strName.Equals("VKEY", StrUtil.CaseIgnoreCmp) ||
                strName.Equals("VKEY-NX", StrUtil.CaseIgnoreCmp) ||
                strName.Equals("VKEY-EX", StrUtil.CaseIgnoreCmp))
            {
                if (!ouParam.HasValue)
                {
                    Debug.Assert(false); return(null);
                }

                SiEvent si = new SiEvent();
                si.Type = SiEventType.Key;
                si.VKey = (int)ouParam.Value;

                if (strName.EndsWith("-NX", StrUtil.CaseIgnoreCmp))
                {
                    si.ExtendedKey = false;
                }
                else if (strName.EndsWith("-EX", StrUtil.CaseIgnoreCmp))
                {
                    si.ExtendedKey = true;
                }

                l.Add(si);
                return(l);
            }
            if (strName.Equals("APPACTIVATE", StrUtil.CaseIgnoreCmp))
            {
                SiEvent si = new SiEvent();
                si.Type = SiEventType.AppActivate;
                si.Text = strParams;

                l.Add(si);
                return(l);
            }
            if (strName.Equals("BEEP", StrUtil.CaseIgnoreCmp))
            {
                SiEvent si = new SiEvent();
                si.Type = SiEventType.Beep;
                si.Text = strParams;

                l.Add(si);
                return(l);
            }

            SiCode siCode = SiCodes.Get(strName);

            SiEvent siTmpl = new SiEvent();

            if (siCode != null)
            {
                siTmpl.Type        = SiEventType.Key;
                siTmpl.VKey        = siCode.VKey;
                siTmpl.ExtendedKey = siCode.ExtKey;
            }
            else if (strName.Length == 1)
            {
                siTmpl.Type = SiEventType.Char;
                siTmpl.Char = strName[0];
            }
            else
            {
                throw new FormatException(KPRes.AutoTypeUnknownPlaceholder +
                                          MessageService.NewLine + @"{" + strName + @"}");
            }

            uint uRepeat = 1;

            if (ouParam.HasValue)
            {
                uRepeat = ouParam.Value;
            }

            for (uint u = 0; u < uRepeat; ++u)
            {
                SiEvent si = new SiEvent();
                si.Type        = siTmpl.Type;
                si.VKey        = siTmpl.VKey;
                si.ExtendedKey = siTmpl.ExtendedKey;
                si.Char        = siTmpl.Char;

                l.Add(si);
            }

            return(l);
        }
Example #7
0
        private static List <SiEvent> Parse(string strSequence)
        {
            CharStream     cs       = new CharStream(strSequence);
            List <SiEvent> l        = new List <SiEvent>();
            string         strError = KPRes.AutoTypeSequenceInvalid;

            Keys kCurKbMods = Keys.None;

            List <Keys> lMods = new List <Keys>();

            lMods.Add(Keys.None);

            while (true)
            {
                char ch = cs.ReadChar();
                if (ch == char.MinValue)
                {
                    break;
                }

                if ((ch == '+') || (ch == '^') || (ch == '%'))
                {
                    if (lMods.Count == 0)
                    {
                        Debug.Assert(false); break;
                    }
                    else if (ch == '+')
                    {
                        lMods[lMods.Count - 1] |= Keys.Shift;
                    }
                    else if (ch == '^')
                    {
                        lMods[lMods.Count - 1] |= Keys.Control;
                    }
                    else if (ch == '%')
                    {
                        lMods[lMods.Count - 1] |= Keys.Alt;
                    }
                    else
                    {
                        Debug.Assert(false);
                    }

                    continue;
                }
                else if (ch == '(')
                {
                    lMods.Add(Keys.None);
                    continue;
                }
                else if (ch == ')')
                {
                    if (lMods.Count >= 2)
                    {
                        lMods.RemoveAt(lMods.Count - 1);
                        lMods[lMods.Count - 1] = Keys.None;
                    }
                    else
                    {
                        throw new FormatException(strError);
                    }

                    continue;
                }

                Keys kEffMods = Keys.None;
                foreach (Keys k in lMods)
                {
                    kEffMods |= k;
                }

                EnsureKeyModifiers(kEffMods, ref kCurKbMods, l);

                if (ch == '{')
                {
                    List <SiEvent> lSub = ParseSpecial(cs);
                    if (lSub == null)
                    {
                        throw new FormatException(strError);
                    }

                    l.AddRange(lSub);
                }
                else if (ch == '}')
                {
                    throw new FormatException(strError);
                }
                else if (ch == '~')
                {
                    SiEvent si = new SiEvent();
                    si.Type = SiEventType.Key;
                    si.VKey = (int)Keys.Enter;

                    l.Add(si);
                }
                else
                {
                    SiEvent si = new SiEvent();
                    si.Type = SiEventType.Char;
                    si.Char = ch;

                    l.Add(si);
                }

                lMods[lMods.Count - 1] = Keys.None;
            }

            EnsureKeyModifiers(Keys.None, ref kCurKbMods, l);

            return(l);
        }
Example #8
0
        private static List<SiEvent> ParseSpecial(CharStream cs)
        {
            // Skip leading white space
            while(true)
            {
                char ch = cs.PeekChar();
                if(ch == char.MinValue) { Debug.Assert(false); return null; }

                if(!char.IsWhiteSpace(ch)) break;
                cs.ReadChar();
            }

            // First char is *always* part of the name (support for "{{}", etc.)
            char chFirst = cs.ReadChar();
            if(chFirst == char.MinValue) { Debug.Assert(false); return null; }

            int iPart = 0;
            StringBuilder sbName = new StringBuilder(), sbParams =
                new StringBuilder();
            sbName.Append(chFirst);

            while(true)
            {
                char ch = cs.ReadChar();
                if(ch == char.MinValue) { Debug.Assert(false); return null; }
                if(ch == '}') break;

                if(iPart == 0)
                {
                    if(char.IsWhiteSpace(ch)) ++iPart;
                    else sbName.Append(ch);
                }
                else sbParams.Append(ch);
            }

            string strName = sbName.ToString();
            string strParams = sbParams.ToString().Trim();

            uint? ouParam = null;
            if(strParams.Length > 0)
            {
                uint uParamTry;
                if(uint.TryParse(strParams, out uParamTry)) ouParam = uParamTry;
            }

            List<SiEvent> l = new List<SiEvent>();

            if(strName.Equals("DELAY", StrUtil.CaseIgnoreCmp))
            {
                if(!ouParam.HasValue) { Debug.Assert(false); return null; }

                SiEvent si = new SiEvent();
                si.Type = SiEventType.Delay;
                si.Delay = ouParam.Value;

                l.Add(si);
                return l;
            }
            if(strName.StartsWith("DELAY=", StrUtil.CaseIgnoreCmp))
            {
                SiEvent si = new SiEvent();
                si.Type = SiEventType.SetDefaultDelay;

                string strDelay = strName.Substring(6).Trim();
                uint uDelay;
                if(uint.TryParse(strDelay, out uDelay))
                    si.Delay = uDelay;
                else { Debug.Assert(false); return null; }

                l.Add(si);
                return l;
            }
            if(strName.Equals("VKEY", StrUtil.CaseIgnoreCmp) ||
                strName.Equals("VKEY-NX", StrUtil.CaseIgnoreCmp) ||
                strName.Equals("VKEY-EX", StrUtil.CaseIgnoreCmp))
            {
                if(!ouParam.HasValue) { Debug.Assert(false); return null; }

                SiEvent si = new SiEvent();
                si.Type = SiEventType.Key;
                si.VKey = (int)ouParam.Value;

                if(strName.EndsWith("-NX", StrUtil.CaseIgnoreCmp))
                    si.ExtendedKey = false;
                else if(strName.EndsWith("-EX", StrUtil.CaseIgnoreCmp))
                    si.ExtendedKey = true;

                l.Add(si);
                return l;
            }
            if(strName.Equals("APPACTIVATE", StrUtil.CaseIgnoreCmp))
            {
                SiEvent si = new SiEvent();
                si.Type = SiEventType.AppActivate;
                si.Text = strParams;

                l.Add(si);
                return l;
            }

            SiCode siCode = SiCodes.Get(strName);

            SiEvent siTmpl = new SiEvent();
            if(siCode != null)
            {
                siTmpl.Type = SiEventType.Key;
                siTmpl.VKey = siCode.VKey;
                siTmpl.ExtendedKey = siCode.ExtKey;
            }
            else if(strName.Length == 1)
            {
                siTmpl.Type = SiEventType.Char;
                siTmpl.Char = strName[0];
            }
            else
            {
                throw new FormatException(KPRes.AutoTypeUnknownPlaceholder +
                    MessageService.NewLine + @"{" + strName + @"}");
            }

            uint uRepeat = 1;
            if(ouParam.HasValue) uRepeat = ouParam.Value;

            for(uint u = 0; u < uRepeat; ++u)
            {
                SiEvent si = new SiEvent();
                si.Type = siTmpl.Type;
                si.VKey = siTmpl.VKey;
                si.ExtendedKey = siTmpl.ExtendedKey;
                si.Char = siTmpl.Char;

                l.Add(si);
            }

            return l;
        }
Example #9
0
        private static void AppActivate(SiEvent si)
        {
            try
            {
                if(string.IsNullOrEmpty(si.Text)) return;

                IntPtr h = NativeMethods.FindWindow(si.Text);
                if(h != IntPtr.Zero)
                    NativeMethods.EnsureForegroundWindow(h);
            }
            catch(Exception) { Debug.Assert(false); }
        }
Example #10
0
        private static List<SiEvent> Parse(string strSequence)
        {
            CharStream cs = new CharStream(strSequence);
            List<SiEvent> l = new List<SiEvent>();
            string strError = KPRes.AutoTypeSequenceInvalid;

            Keys kCurKbMods = Keys.None;

            List<Keys> lMods = new List<Keys>();
            lMods.Add(Keys.None);

            while(true)
            {
                char ch = cs.ReadChar();
                if(ch == char.MinValue) break;

                if((ch == '+') || (ch == '^') || (ch == '%'))
                {
                    if(lMods.Count == 0) { Debug.Assert(false); break; }
                    else if(ch == '+') lMods[lMods.Count - 1] |= Keys.Shift;
                    else if(ch == '^') lMods[lMods.Count - 1] |= Keys.Control;
                    else if(ch == '%') lMods[lMods.Count - 1] |= Keys.Alt;
                    else { Debug.Assert(false); }

                    continue;
                }
                else if(ch == '(')
                {
                    lMods.Add(Keys.None);
                    continue;
                }
                else if(ch == ')')
                {
                    if(lMods.Count >= 2)
                    {
                        lMods.RemoveAt(lMods.Count - 1);
                        lMods[lMods.Count - 1] = Keys.None;
                    }
                    else throw new FormatException(strError);

                    continue;
                }

                Keys kEffMods = Keys.None;
                foreach(Keys k in lMods) kEffMods |= k;

                EnsureKeyModifiers(kEffMods, ref kCurKbMods, l);

                if(ch == '{')
                {
                    List<SiEvent> lSub = ParseSpecial(cs);
                    if(lSub == null) throw new FormatException(strError);

                    l.AddRange(lSub);
                }
                else if(ch == '}')
                    throw new FormatException(strError);
                else if(ch == '~')
                {
                    SiEvent si = new SiEvent();
                    si.Type = SiEventType.Key;
                    si.VKey = (int)Keys.Enter;

                    l.Add(si);
                }
                else
                {
                    SiEvent si = new SiEvent();
                    si.Type = SiEventType.Char;
                    si.Char = ch;

                    l.Add(si);
                }

                lMods[lMods.Count - 1] = Keys.None;
            }

            EnsureKeyModifiers(Keys.None, ref kCurKbMods, l);

            return l;
        }
Example #11
0
 private static bool IsClipboardOp(SiEvent si)
 {
     if(si == null) { Debug.Assert(false); return false; }
     return (si.Type == SiEventType.ClipboardCopy);
 }
Example #12
0
        private static void EnsureKeyModifiers(Keys kReqMods, ref Keys kCurKbMods,
            List<SiEvent> l)
        {
            if(kReqMods == kCurKbMods) return;

            if((kReqMods & Keys.Shift) != (kCurKbMods & Keys.Shift))
            {
                SiEvent si = new SiEvent();
                si.Type = SiEventType.KeyModifier;
                si.KeyModifier = Keys.Shift;
                si.Down = ((kReqMods & Keys.Shift) != Keys.None);

                l.Add(si);
            }

            if((kReqMods & Keys.Control) != (kCurKbMods & Keys.Control))
            {
                SiEvent si = new SiEvent();
                si.Type = SiEventType.KeyModifier;
                si.KeyModifier = Keys.Control;
                si.Down = ((kReqMods & Keys.Control) != Keys.None);

                l.Add(si);
            }

            if((kReqMods & Keys.Alt) != (kCurKbMods & Keys.Alt))
            {
                SiEvent si = new SiEvent();
                si.Type = SiEventType.KeyModifier;
                si.KeyModifier = Keys.Alt;
                si.Down = ((kReqMods & Keys.Alt) != Keys.None);

                l.Add(si);
            }

            kCurKbMods = kReqMods;
        }
Example #13
0
        private static void Beep(SiEvent si)
        {
            try
            {
                string str = si.Text;
                if(string.IsNullOrEmpty(str))
                {
                    SystemSounds.Beep.Play();
                    return;
                }

                string[] v = str.Split(new char[] { ' ', '\t' },
                    StringSplitOptions.RemoveEmptyEntries);

                int f = 800, d = 200; // Defaults of Console.Beep()
                if(v.Length >= 1) int.TryParse(v[0], out f);
                if(v.Length >= 2) int.TryParse(v[1], out d);

                f = Math.Min(Math.Max(f, 37), 32767);
                if(d <= 0) return;

                Console.Beep(f, d); // Throws on a server
            }
            catch(Exception) { Debug.Assert(false); }
        }
Example #14
0
        private static SiEvent CreateVKeyEvent(string strParams)
        {
            string[] v = (strParams ?? string.Empty).Trim().Split(
                new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if ((v == null) || (v.Length < 1))
            {
                Debug.Assert(false); return(null);
            }

            SiEvent si = new SiEvent();

            si.Type = SiEventType.Key;

            int k;

            if (!StrUtil.TryParseInt(v[0], out k))
            {
                Debug.Assert(false); return(null);
            }
            if (k < 0)
            {
                Debug.Assert(false); return(null);
            }
            si.VKey = k;

            for (int i = 1; i < v.Length; ++i)
            {
                string strParam = v[i];
                if (string.IsNullOrEmpty(strParam))
                {
                    Debug.Assert(false); continue;
                }

                if (strParam.IndexOf('=') < 0)
                {
                    bool bExt    = (strParam.IndexOf('E') >= 0);
                    bool bNonExt = (strParam.IndexOf('N') >= 0);
                    if (bExt && !bNonExt)
                    {
                        si.ExtendedKey = true;
                    }
                    if (!bExt && bNonExt)
                    {
                        si.ExtendedKey = false;
                    }
                    // The default is null => automatic decision

                    bool bDown = (strParam.IndexOf('D') >= 0);
                    bool bUp   = (strParam.IndexOf('U') >= 0);
                    if (bDown && !bUp)
                    {
                        si.Down = true;
                    }
                    if (!bDown && bUp)
                    {
                        si.Down = false;
                    }
                    // The default is null => down and up
                }

                // Named parameters with values could be implemented like
                // {VKEY X ED R=5 DA=100}, and parameters without a '='
                // could be treated as flags (so {VKEY X ED} = {VKEY X E D})
            }

            return(si);
        }