Inheritance: MonoBehaviour
 private void OnAboutActionExecuted(object sender, MenuActionEventArgs e)
 {
     var dialog = new AboutUI();
     dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
     dialog.Topmost = true;
     dialog.ShowDialog();
 }
Exemplo n.º 2
0
        public Context()
        {
            _Components = new Container();

            // Load Settings, but first create if not exist
            if (!File.Exists(@"config.ini"))
            {
                var height = SystemInformation.VirtualScreen.Height;
                File.WriteAllText(@"config.ini",
                                  "[Settings]" + Environment.NewLine +
                                  "displayindex=2" + Environment.NewLine +
                                  "xaxis=20" + Environment.NewLine +
                                  "yaxis=" + (height - new Display().Height - 60) + Environment.NewLine +
                                  "displaytime=2" + Environment.NewLine +
                                  "notify=True" + Environment.NewLine +
                                  "logkeys=True" + Environment.NewLine +
                                  "stdkeys=True" + Environment.NewLine);
            }

            if (File.Exists(@"config.ini"))
            {
                var myini = new IniFile(@"config.ini");

                _Location = new System.Drawing.Point(
                    Convert.ToInt16(myini.Read("xaxis", "Settings")),
                    Convert.ToInt16(myini.Read("yaxis", "Settings"))
                    );

                _DisplayTime = Convert.ToInt16(myini.Read("displaytime", "Settings"));
                _Notify      = Convert.ToBoolean(myini.Read("notify", "Settings"));
                _StdKeys     = Convert.ToBoolean(myini.Read("stdkeys", "Settings"));
                _LogKeys     = Convert.ToBoolean(myini.Read("logkeys", "Settings"));
            }

            _KeyUi          = new Display();
            _KeyUi.Location = _Location;

            _ContextMenustrip = new ContextMenuStrip();
            _ContextMenustrip.Items.Add(NewToolStripItem("Stop recording", (o, s) =>
            {
                var firstItem = _ContextMenustrip.Items[0];

                if (_Record)
                {
                    // Stop Recording
                    _Record        = false;
                    firstItem.Text = "Start recording";
                    DisplayStatusMessage("Kling : Service stopped");
                }
                else
                {
                    // Start Recording
                    _Record        = true;
                    firstItem.Text = "Stop recording";
                    DisplayStatusMessage("Kling : Service started");
                }
            }));

            _ContextMenustrip.Items.Add(new ToolStripSeparator());
            _ContextMenustrip.Items.Add(NewToolStripItem("Settings", ShowSettings));
            _ContextMenustrip.Items.Add(NewToolStripItem("Restart", (o, s) => { System.Windows.Forms.Application.Restart(); }));
            _ContextMenustrip.Items.Add(NewToolStripItem("About", (o, s) =>
            {
                // About screen dialog
                if (!_IsAboutShowing)
                {
                    _IsAboutShowing = true;
                    AboutUI ui      = new AboutUI();
                    ui.Closing     += (obj, ex) => { _IsAboutShowing = false; };
                    ui.ShowDialog();
                }
            }));

            _ContextMenustrip.Items.Add(new ToolStripSeparator());
            _ContextMenustrip.Items.Add(NewToolStripItem("Exit", (o, s) =>
            {
                // Exit Button
                if (_EventHookFactory != null)
                {
                    _EventHookFactory.Dispose();
                }
                System.Windows.Forms.Application.Exit();
            }));

            _NotifyIcon = new NotifyIcon(_Components)
            {
                ContextMenuStrip = _ContextMenustrip,
                Icon             = Properties.Resources.icon,
                Text             = "Kling",
                Visible          = true,
            };

            _NotifyIcon.DoubleClick += ShowSettings;

            _HiddenWindow = new Window();
            _HiddenWindow.Hide();
            DisplayStatusMessage(_NotifyIcon.Text + ": Start pressing keys");

            _KeyboardWatcher = _EventHookFactory.GetKeyboardWatcher();
            _KeyboardWatcher.OnKeyboardInput += (s, e) =>
            {
                if (!_Record)
                {
                    return;
                }

                if (_MacroEvents != null)
                {
                    _MacroEvents.Add(e);
                }

                if (e.KeyMouseEventType == MacroEventType.KeyPress)
                {
                    var keyEvent = (KeyPressEventArgs)e.EventArgs;

                    if (e.KeyMouseEventType.ToString().Contains("KeyUp"))
                    {
                        // This will also show a form
                        DisplayKeys(getKeys(keyEvent.KeyChar.ToString()));
                    }
                }
                else
                {
                    var keyEvent = (KeyEventArgs)e.EventArgs;

                    if (e.KeyMouseEventType.ToString().Contains("KeyUp"))
                    {
                        // This will show a form
                        var keys = keyEvent.KeyCode;

                        // Suppress next event
                        if (_SuppressKey)
                        {
                            _SuppressKey = false;
                            return;
                        }

                        if (_SuppressKey2)
                        {
                            _SuppressKey2 = false;
                            return;
                        }

                        if (isControlPressed(keyEvent, keys))
                        {
                            _SpecialKeys = true;
                            if (isShiftPressed(keyEvent, keys))
                            {
                                if (isAltPressed(keyEvent, keys))
                                {
                                    _SuppressKey2 = true;
                                    showKey("Ctrl + Shift + Alt + ", keys);
                                }
                                else
                                {
                                    showKey("Ctrl + Shift + ", keys);
                                }
                            }
                            else if (isAltPressed(keyEvent, keys))
                            {
                                if (isShiftPressed(keyEvent, keys))
                                {
                                    _SuppressKey2 = true;
                                    showKey("Ctrl + Alt + Shift + ", keys);
                                }
                                else
                                {
                                    showKey("Ctrl + Alt + ", keys);
                                }
                            }
                            else
                            {
                                DisplayKeys("Ctrl + " + getKeys(keys.ToString()));
                            }
                        }
                        else if (isAltPressed(keyEvent, keys))
                        {
                            _SpecialKeys = true;
                            if (isShiftPressed(keyEvent, keys))
                            {
                                if (isControlPressed(keyEvent, keys))
                                {
                                    _SuppressKey2 = true;
                                    showKey("Alt + Shift + Ctrl + ", keys);
                                }
                                else
                                {
                                    showKey("Alt + Shift + ", keys);
                                }
                            }
                            else if (isControlPressed(keyEvent, keys))
                            {
                                if (isShiftPressed(keyEvent, keys))
                                {
                                    _SuppressKey2 = true;
                                    showKey("Alt + Ctrl + Shift + ", keys);
                                }
                                else
                                {
                                    showKey("Alt + Ctrl + ", keys);
                                }
                            }
                            else
                            {
                                DisplayKeys("Alt + " + getKeys(keys.ToString()));
                            }
                        }
                        else if (isShiftPressed(keyEvent, keys))
                        {
                            _SpecialKeys = true;
                            if (isControlPressed(keyEvent, keys))
                            {
                                if (isAltPressed(keyEvent, keys))
                                {
                                    _SuppressKey2 = true;
                                    showKey("Shift + Ctrl + Alt + ", keys);
                                }
                                else
                                {
                                    showKey("Shift + Ctrl + ", keys);
                                }
                            }
                            else if (isAltPressed(keyEvent, keys))
                            {
                                if (isControlPressed(keyEvent, keys))
                                {
                                    _SuppressKey2 = true;
                                    showKey("Shift + Alt + Ctrl + ", keys);
                                }
                                else
                                {
                                    showKey("Shift + Alt + ", keys);
                                }
                            }
                            else
                            {
                                DisplayKeys("Shift + " + getKeys(keys.ToString()));
                            }
                        }
                        else
                        {
                            if (!_SpecialKeys)
                            {
                                DisplayKeys(getKeys(keys.ToString()));
                            }
                            else
                            {
                                _SpecialKeys = false;
                            }
                        }
                    }
                }
            };

            _MacroEvents = new List <MacroEvent>();
            _KeyboardWatcher.Start(Hook.GlobalEvents());

            _Timer          = new Timer();
            _Timer.Interval = _DisplayTime * 1000;
            _Timer.Tick    += async(o, e) =>
            {
                _Timer.Stop();
                while (_KeyUi.Opacity > 0.0)
                {
                    await Task.Delay(20);

                    _KeyUi.Opacity -= 0.05;
                }
                _KeyUi.Opacity = 0;
                _KeyUi.Hide();
                _KeyUi.Opacity = 0.7;
            };
        }