What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

Highvoltz's KeyBind Decorator (for devs only)

highvoltz

Well-Known Member
Joined
Mar 22, 2010
Messages
1,729
Reaction score
141
If you want to add keybind capability to your project then this might help you.

Introducing the new KeyBindDecorator ...

Basically this is a Decorator that takes a keybind System.Windows.Forms.Keys and Composite child in the constructor parameter. If that key or key combination is pressed then the child gets executed.

If you use this in your project then I expect my name in the credits list.

Edit
: I included a sample project showing how it can be used. basically just a framework.

Code:
    [COLOR=green]// Made by Highvoltz[/COLOR]

    [COLOR=blue]public[/COLOR] [COLOR=blue]class[/COLOR] [COLOR=#2b91af]KeyBindDecorator[/COLOR] : [COLOR=#2b91af]Decorator[/COLOR]
    {
        [[COLOR=#2b91af]DllImport[/COLOR]([COLOR=#a31515]"user32.dll"[/COLOR])]
        [COLOR=blue]static[/COLOR] [COLOR=blue]extern[/COLOR] [COLOR=blue]short[/COLOR] GetAsyncKeyState(System.Windows.Forms.[COLOR=#2b91af]Keys[/COLOR] vKey);
        [[COLOR=#2b91af]DllImport[/COLOR]([COLOR=#a31515]"user32.dll"[/COLOR])]
        [COLOR=blue]static[/COLOR] [COLOR=blue]extern[/COLOR] [COLOR=#2b91af]IntPtr[/COLOR] GetForegroundWindow();
        [COLOR=blue]private[/COLOR] [COLOR=blue]static[/COLOR] [COLOR=#2b91af]IntPtr[/COLOR] _wowHandle = [COLOR=#2b91af]ObjectManager[/COLOR].WoWProcess.MainWindowHandle;
        [COLOR=blue]private[/COLOR] System.Windows.Forms.[COLOR=#2b91af]Keys[/COLOR] _keyBind;
        [COLOR=green]// these modifer bools are true if our keybind uses them. otherwise false.[/COLOR]
        [COLOR=blue]private[/COLOR] [COLOR=blue]bool[/COLOR] _shiftkey = [COLOR=blue]false[/COLOR];
        [COLOR=blue]private[/COLOR] [COLOR=blue]bool[/COLOR] _ctrlkey = [COLOR=blue]false[/COLOR];
        [COLOR=blue]private[/COLOR] [COLOR=blue]bool[/COLOR] _altkey = [COLOR=blue]false[/COLOR];
        [COLOR=blue]private[/COLOR] [COLOR=#2b91af]Keys[/COLOR] _lastKey = [COLOR=#2b91af]Keys[/COLOR].None;
        [COLOR=blue]public[/COLOR] KeyBindDecorator([COLOR=#2b91af]Keys[/COLOR] keybind, [COLOR=#2b91af]Composite[/COLOR] child) : [COLOR=blue]this[/COLOR](keybind, child, [COLOR=blue]true[/COLOR]) { }
     
        [COLOR=blue]public[/COLOR] KeyBindDecorator([COLOR=#2b91af]Keys[/COLOR] keybind, [COLOR=#2b91af]Composite[/COLOR] child,[COLOR=blue]bool[/COLOR] requireWowInForground)
            : [COLOR=blue]base[/COLOR](child)
        {
            [COLOR=green]// k holds both modifer and  key so we need to extract the modifer[/COLOR]
            _shiftkey = (keybind & [COLOR=#2b91af]Keys[/COLOR].Shift) != 0 ? [COLOR=blue]true[/COLOR] : [COLOR=blue]false[/COLOR];
            _ctrlkey = (keybind & [COLOR=#2b91af]Keys[/COLOR].Control) != 0 ? [COLOR=blue]true[/COLOR] : [COLOR=blue]false[/COLOR];
            _altkey = (keybind & [COLOR=#2b91af]Keys[/COLOR].Alt) != 0 ? [COLOR=blue]true[/COLOR] : [COLOR=blue]false[/COLOR];
            [COLOR=green]// now we remove the modifer flags from keybind and assign it to _keybind.[/COLOR]
            _keyBind = (~[COLOR=#2b91af]Keys[/COLOR].Modifiers) & keybind;
            RequireWowInForground = requireWowInForground;
        }
        [COLOR=blue]public[/COLOR] System.Windows.Forms.[COLOR=#2b91af]Keys[/COLOR] KeyBind
        {
            [COLOR=blue]get[/COLOR] { [COLOR=blue]return[/COLOR] _keyBind; }
            [COLOR=blue]set[/COLOR]
            {   [COLOR=green]// check for modifer flags [/COLOR]
                _shiftkey = ([COLOR=blue]value[/COLOR] & [COLOR=#2b91af]Keys[/COLOR].Shift) != 0 ? [COLOR=blue]true[/COLOR] : [COLOR=blue]false[/COLOR];
                _ctrlkey = ([COLOR=blue]value[/COLOR] & [COLOR=#2b91af]Keys[/COLOR].Control) != 0 ? [COLOR=blue]true[/COLOR] : [COLOR=blue]false[/COLOR];
                _altkey = ([COLOR=blue]value[/COLOR] & [COLOR=#2b91af]Keys[/COLOR].Alt) != 0 ? [COLOR=blue]true[/COLOR] : [COLOR=blue]false[/COLOR];
                [COLOR=green]// remove modifer bits[/COLOR]
                _keyBind = (~[COLOR=#2b91af]Keys[/COLOR].Modifiers) & [COLOR=blue]value[/COLOR];
            }
        }
[COLOR=gray]        [COLOR=blue]public[/COLOR] [COLOR=blue]static[/COLOR] [COLOR=blue]bool[/COLOR] IsKeyDown(System.Windows.Forms.[COLOR=#2b91af]Keys[/COLOR] vKey)
        {
            [COLOR=blue]return[/COLOR] (GetAsyncKeyState(vKey) != 0);
        }
        [COLOR=blue]public[/COLOR] [COLOR=blue]bool[/COLOR] RequireWowInForground { [COLOR=blue]get[/COLOR]; [COLOR=blue]set[/COLOR]; }
        [COLOR=blue]public[/COLOR] [COLOR=blue]bool[/COLOR] PollKeys()
        {   [COLOR=green]// checks button status and returns true if our beybind is pressed.[/COLOR]
            [COLOR=blue]if[/COLOR] (IsKeyDown(_keyBind) && _lastKey != _keyBind && (!(_shiftkey ^ IsKeyDown([COLOR=#2b91af]Keys[/COLOR].ShiftKey))) &&
                (!(_ctrlkey ^ IsKeyDown([COLOR=#2b91af]Keys[/COLOR].ControlKey))) && (!(_altkey ^ IsKeyDown([COLOR=#2b91af]Keys[/COLOR].Menu))))
            {
                _lastKey = _keyBind;
                [COLOR=blue]return[/COLOR] [COLOR=blue]true[/COLOR];
            }
            [COLOR=blue]if[/COLOR] (!IsKeyDown(_keyBind))
            {
                _lastKey = [COLOR=#2b91af]Keys[/COLOR].None;
            }
            [COLOR=blue]return[/COLOR] [COLOR=blue]false[/COLOR];
        }
 
        [COLOR=blue]protected[/COLOR] [COLOR=blue]override[/COLOR] [COLOR=blue]bool[/COLOR] CanRun([COLOR=blue]object[/COLOR] context)
        {
            [COLOR=green]// if keybind is pressed and our wow window is the currently active window then execute macro[/COLOR]
            [COLOR=#2b91af]IntPtr[/COLOR] fgWinHwnd = GetForegroundWindow();[/COLOR]            
            [COLOR=blue]if[/COLOR] (PollKeys() && (!RequireWowInForground || (RequireWowInForground && _wowHandle == fgWinHwnd)) )
[COLOR=gray]                 [COLOR=blue]return[/COLOR] [COLOR=blue]true[/COLOR];
            [COLOR=blue]else[/COLOR]
                [COLOR=blue]return[/COLOR] [COLOR=blue]false[/COLOR];
        }
    }[/COLOR]

 

Attachments

Last edited:
Added changes so the action doesn't get spammed when keybind is being pressed down, thanks to Comyn for bringing to my attention. Also added a constructor that accepts an additional parameter, a bool when set to true requires the WoW window that HB is attached to, to be in the foreground.
 
Back
Top