Author Topic: EA Addin Key Shortcut  (Read 8491 times)

McMannus

  • EA User
  • **
  • Posts: 108
  • Karma: +4/-1
    • View Profile
Re: EA Addin Key Shortcut
« Reply #15 on: August 07, 2014, 02:30:12 am »
Hi guys,

just to keep you informed about a significant improvement that I discovered during searching the documentation:

The approach for figuring out the currently active application was solved by the background worker thread running infinitely. In the field, this caused ~50% CPU occupation during idle time.

The idea is to solve that by reacting to window events.

Just replace the WndProc method in InvisibleHotkeyForm class with the following code:

Code: [Select]
       protected override void WndProc(ref Message m)
        {
            if (m.Msg == Hotkey.WM_HOTKEY_MSG_ID)
            {
                foreach (Hotkey key in _hotkeys)
                {
                    if (key.IsPressedKeyCombination(m.LParam))
                    {
                        key.Handler();
                        break;
                    }
                }
            }
            else if (m.Msg == WM_ACTIVATEAPP || m.Msg == WM_NCACTIVATE)
            {
                bool windowGotFocus = (m.WParam.ToInt32() == 1);
                if (windowGotFocus)
                {
                    RegisterHotKeys();
                }
                else
                {
                    UnregisterHotKeys();
                }
            }
            base.WndProc(ref m);
        }

and at these two constants to the class:
Code: [Select]
       private const int WM_ACTIVATEAPP = 0x001C;
        private const int WM_NCACTIVATE = 0x0086;

Afterwards the ActiveProcess.cs can be deleted as well as all things that are related to the background worker.

In addition, remove the RegisterHotkeys() call from the OnLoad method.

Regards,
Jan
« Last Edit: August 07, 2014, 02:31:18 am by McMannus »

Helmut Ortmann

  • EA User
  • **
  • Posts: 967
  • Karma: +42/-1
    • View Profile
Re: EA Addin Key Shortcut
« Reply #16 on: August 07, 2014, 03:37:45 am »
Hi Jan,

thanks a lot.

Could you explain what you mean by 'as well as all things that are related to the background worker'?

Thanks,

Helmut
Coaching, Training, Workshop (Addins: hoTools, Search&Replace, LineStyle)

McMannus

  • EA User
  • **
  • Posts: 108
  • Karma: +4/-1
    • View Profile
Re: EA Addin Key Shortcut
« Reply #17 on: August 07, 2014, 05:15:43 am »
Code: [Select]
public partial class InvisibleHotKeyForm : Form
    {
        private const int WM_ACTIVATEAPP = 0x001C;
        private const int WM_NCACTIVATE = 0x0086;

        private readonly IEnumerable<Hotkey> _hotkeys;
        private readonly IDualRepository _rep;

        public InvisibleHotKeyForm(IEnumerable<Hotkey> hotkeys, IDualRepository rep)
        {
            InitializeComponent();
            _hotkeys = hotkeys;
            _rep = rep;
            Closing += (sender, eventArgs) => ((List<Hotkey>) _hotkeys).ForEach(key => key.Dispose());
        }

        protected override void OnLoad(EventArgs e)
        {
            Visible = false;
            ShowInTaskbar = false;
            base.OnLoad(e);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Hotkey.WM_HOTKEY_MSG_ID)
            {
                foreach (Hotkey key in _hotkeys)
                {
                    if (key.IsPressedKeyCombination(m.LParam))
                    {
                        key.Handler();
                        break;
                    }
                }
            }
            else if (m.Msg == WM_ACTIVATEAPP || m.Msg == WM_NCACTIVATE)
            {
                bool windowGotFocus = (m.WParam.ToInt32() == 1);
                if (windowGotFocus)
                {
                    RegisterHotKeys();
                }
                else
                {
                    UnregisterHotKeys();
                }
            }
            base.WndProc(ref m);
        }

        private void UnregisterHotKeys()
        {
            if (!_hotkeys.Any()) return;
            foreach (Hotkey key in _hotkeys)
            {
                key.Unregister();
            }
        }

        private void RegisterHotKeys()
        {
            UnregisterHotKeys();
            foreach (Hotkey key in _hotkeys)
            {
                try
                {
                    key.Register(window: this);
                }
                catch (GlobalHotkeyException exc)
                {
                    Debug.WriteLine(exc.Message);
                }
            }
        }
    }

Helmut Ortmann

  • EA User
  • **
  • Posts: 967
  • Karma: +42/-1
    • View Profile
Re: EA Addin Key Shortcut
« Reply #18 on: August 09, 2014, 06:43:41 pm »
Hi Jan,

thanks a lot.

It works!

Helmut
Coaching, Training, Workshop (Addins: hoTools, Search&Replace, LineStyle)