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

[Plugin] HB Toggle

Vhaldir

New Member
Joined
Jul 31, 2010
Messages
1
Reaction score
0
I do not take credit for the original creation of this plugin. All I have done is modify the dll references to provide functionality for the HB Version 2.0.0.3740.

Description: HB Toggle allows the user to define a HotKey so that HB may be stopped/started in-game with the press of a button.

EDIT: 31, Jan 2011 - I have updated the code for better initialization when HB loads.

Code:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Diagnostics;
using System.Configuration;
using System.Threading;
using Styx.Plugins.PluginClass;
using Styx.Logic.BehaviorTree;
using Styx.Helpers;
using Styx.WoWInternals;
using System.IO;

namespace HighVoltz
{
    public partial class FormHB_Toggle : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.KeyBindButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // KeyBindButton
            // 
            this.KeyBindButton.BackColor = System.Drawing.SystemColors.Control;
            this.KeyBindButton.Location = new System.Drawing.Point(7, 12);
            this.KeyBindButton.Name = "KeyBindButton";
            this.KeyBindButton.Size = new System.Drawing.Size(175, 25);
            this.KeyBindButton.TabIndex = 0;
            this.KeyBindButton.Text = "Click to set Keybind";
            this.KeyBindButton.UseVisualStyleBackColor = false;
            this.KeyBindButton.Click += new System.EventHandler(this.KeyBindButton_Click);
            this.KeyBindButton.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyBindButton_KeyDown);
            // 
            // FormHB_Toggle
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(194, 47);
            this.Controls.Add(this.KeyBindButton);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormHB_Toggle";
            this.Text = "Key Binding";
            this.Load += new System.EventHandler(this.FormHB_Toggle_Load);
            this.Shown += new System.EventHandler(this.FormHB_Toggle_Shown);
            this.ResumeLayout(false);

        }

        #endregion
        private bool keyBindMode = false;
        private System.Windows.Forms.Button KeyBindButton;

        public FormHB_Toggle()
        {
            InitializeComponent();
        }

        private void FormHB_Toggle_Load(object sender, EventArgs e)
        {

        }

        private void KeyBindButton_Click(object sender, EventArgs e)
        {
            if (!keyBindMode)
            {
                KeyBindButton.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
                KeyBindButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                KeyBindButton.Text = "Press key combination";
                keyBindMode = true;
            }
        }

        private void KeyBindButton_KeyDown(object sender, KeyEventArgs e)
        {
            // return if not changing keybinds.
            if (!keyBindMode) return;
            // convert modifier keys to string to be displayed on button
            string keybindstring = (e.Shift ? "Shift " : "") + (e.Control ? "Ctrl " : "") + (e.Alt ? "Alt " : "");
            KeyBindButton.Text = keybindstring;
            if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.Menu)
            {
                return;
            }
            // display numbers in button text correctly.
            if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
            {
                keybindstring += e.KeyValue - 48;
            }
            else
            {
                keybindstring += e.KeyCode.ToString();
            }
            KeyBindButton.Text = keybindstring;
            KeyBindButton.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
            KeyBindButton.BackColor = System.Drawing.SystemColors.Control;
            keyBindMode = false;
            HB_Toggle.SetKeyBind(e.KeyData);
        }

        private void FormHB_Toggle_Shown(object sender, EventArgs e)
        {
            // everything here has to do with displaying the current keybind on the button.
            System.Windows.Forms.Keys vKey = HighVoltz.Properties.Settings.Default.Keybind;
            if (vKey == 0)
            {
                KeyBindButton.Text = "Click to set Keybind";
            }
            else
            {
                // extract modifier keys                 
                string keybindstring = ((vKey & Keys.Shift) != 0 ? "Shift " : "") + ((vKey & Keys.Control) != 0 ? "Ctrl " : "") + ((vKey & Keys.Alt) != 0 ? "Alt " : "");
                // Shift = 0x00010000, Ctrl= 0x00020000, Alt = 0x00040000
                // remove modifer bits
                vKey ^= (Keys.Shift & vKey) | (Keys.Control & vKey) | (Keys.Alt & vKey);
                // display numbers in button text correctly.
                switch (vKey)
                {
                    case Keys.D0:
                        keybindstring += "0";
                        break;
                    case Keys.D1:
                        keybindstring += "1";
                        break;
                    case Keys.D2:
                        keybindstring += "2";
                        break;
                    case Keys.D3:
                        keybindstring += "3";
                        break;
                    case Keys.D4:
                        keybindstring += "4";
                        break;
                    case Keys.D5:
                        keybindstring += "5";
                        break;
                    case Keys.D6:
                        keybindstring += "6";
                        break;
                    case Keys.D7:
                        keybindstring += "7";
                        break;
                    case Keys.D8:
                        keybindstring += "8";
                        break;
                    case Keys.D9:
                        keybindstring += "9";
                        break;
                    default:
                        keybindstring += vKey.ToString();
                        break;
                }
                KeyBindButton.Text = keybindstring;
            }
        }
    }

    namespace HighVoltz.Properties
    {


        [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
        public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
        {

            private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

            public static Settings Default
            {
                get
                {
                    return defaultInstance;
                }
            }

            [global::System.Configuration.UserScopedSettingAttribute()]
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.Configuration.DefaultSettingValueAttribute("None")]
            public global::System.Windows.Forms.Keys Keybind
            {
                get
                {
                    return ((global::System.Windows.Forms.Keys)(this["Keybind"]));
                }
                set
                {
                    this["Keybind"] = value;
                }
            }
        }
    }

    
    public class KeyBindClass
    {
        [DllImport("user32.dll")]
        static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
        private System.Windows.Forms.Keys _keyBind;
        // these modifer bools are true if our keybind uses them. otherwise false.
        private bool _shiftkey = false;
        private bool _ctrlkey = false;
        private bool _altkey = false;
        public KeyBindClass()
        {
            _keyBind = new System.Windows.Forms.Keys();
        }
 
        public KeyBindClass(System.Windows.Forms.Keys k)
        {  // k holds both modifer and  key so we need to extract the modifer
            _shiftkey = (k & Keys.Shift) != 0 ? true : false;
            _ctrlkey = (k & Keys.Control) != 0 ? true : false;
            _altkey = (k & Keys.Alt) != 0 ? true : false;
            // now we remove the modifer flags from k and assign it to _keybind.
            _keyBind = (Keys.Shift & k) | (Keys.Control & k) | (Keys.Alt & k);
        }

        public System.Windows.Forms.Keys KeyBind
        {
            get { return _keyBind; }
            set
            {   // check for modifer flags 
                _shiftkey = (value & Keys.Shift) != 0 ? true : false;
                _ctrlkey = (value & Keys.Control) != 0 ? true : false;
                _altkey = (value & Keys.Alt) != 0 ? true : false;
                // remove modifer bits
                value ^= (Keys.Shift & value) | (Keys.Control & value) | (Keys.Alt & value);
                _keyBind = value;
            }
        }

        public static bool IsKeyDown(System.Windows.Forms.Keys vKey)
        {

            return (GetAsyncKeyState(vKey) != 0);
        }

        public bool PollKeys()
        {   // checks button status and returns true if our beybind is pressed.
            if (IsKeyDown(_keyBind) && (!(_shiftkey ^ IsKeyDown(Keys.ShiftKey))) &&
                (!(_ctrlkey ^ IsKeyDown(Keys.ControlKey))) && (!(_altkey ^ IsKeyDown(Keys.Menu))))
            {
                return true;
            }
            return false;
        }
    }


    public class HB_Toggle : HBPlugin
    {
        static public string name = "HB Toggle";
        private readonly FormHB_Toggle _configForm = new FormHB_Toggle();
        public override string Name { get { return "HB Toggle"; } }
        public override string Author { get { return "HighVoltz"; } }
        private readonly Version _version = new Version(1, 2);
        public override Version Version { get { return _version; } }
        public override string ButtonText { get { return "KeyBinding"; } }
        public override bool WantButton { get { return true; } }
        private static KeyBindClass _keyBind = new KeyBindClass();
        public HB_Toggle()
        {
            Logging.Write("Loading file");
            Logging.Write("Load file");
            Logging.Write(Name + " Initializing");
            InitHB_Toggle();
        }
        // making a new thread because pulse() doesn't get called when bot is paused and it doesn't get called frequently 
        // enough to get acurrate key readings. thread terminates when HB terminates.
        static Thread keyPollThread = new Thread(new ThreadStart(ThreadLoop));
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();
        private static IntPtr _wowHandle = ObjectManager.WoWProcess.MainWindowHandle;
        // contains info about this plugin, used to determine if this plugin is enabled from my thread loop.
        private static Styx.Plugins.PluginContainer myPlugin;
        
         public override void OnButtonPress()
        {
            _configForm.ShowDialog();
        }

	private bool _initialized;
        public override void Pulse()
        {
            if (!_initialized)
            {
                InitHB_Toggle();
                _initialized = true;
            }
        }

        public void InitHB_Toggle()
        {
            loadSettings();
            keyPollThread.IsBackground = true;
            Logging.OnWrite += ReadLog;
            Logging.Write("Starting Thread: keyPollThread");
            keyPollThread.Start();
	    _initialized = true;
        }

        // Thanks to Cowdude for this. it detects recompile and terminates thread.
        void ReadLog(string line, System.Drawing.Color c)
        {
            Logging.WriteDebug("ReadLog: " + line);
            if (line == this.Name + " Initializing")
            {
                Logging.OnWrite -= ReadLog;
                //InitHB_Toggle();// init HB Toggle before aborting this thread.
                Logging.Write("Recompile detected");
                keyPollThread.Abort();
                // wait for thread to abort.
                while (keyPollThread.IsAlive)
                {
                    Logging.Write("Waiting for thread to abort");
                    Thread.Sleep(100);
                }
                //Logging.Write("Init from ReadLog()");
                // InitHB_Toggle();
            }
        }


        // this is being run in a new thread
        public static void ThreadLoop()
        {
            bool toggleControl = false;
            // wait till plugin manager fully loads
            while (Styx.Plugins.PluginManager.Plugins.Count() == 0)
                Thread.Sleep(100);
            for (int i = 0; i < Styx.Plugins.PluginManager.Plugins.Count(); i++)
            {
                myPlugin = Styx.Plugins.PluginManager.Plugins[i];
                if (myPlugin.Plugin.Name == "HB Toggle")
                	break;
            }

            while (true)
            {
                // if keybind is pressed and our wow window is the currently active window then toggle HB
                IntPtr fgWinHwnd = GetForegroundWindow();
             
                if (_keyBind.PollKeys() && !toggleControl && (_wowHandle == fgWinHwnd) && myPlugin.Enabled)
                {
                    toggleControl = true;
                    Toggle();
                }
                else
                {
                    toggleControl = false;
                }
                // Yield the rest of the time slice.
                Thread.Sleep(75);
            }
        }



        private static void Toggle()
        {
            if (TreeRoot.IsRunning)
            {
                Logging.Write("Honorbuddy: Off");
                TreeRoot.Stop();
                WoWMovement.MoveStop();
                Lua.DoString("UIErrorsFrame:AddMessage(\"HonorBuddy: OFF\", 1.0, 0.4, 0.0)");

            }
            else if (TreeRoot.Current != null)
            {
                Logging.Write("Honorbuddy: On");
                Lua.DoString("UIErrorsFrame:AddMessage(\"HonorBuddy: ON\", 0.0, 1.0, 0.0)");
                TreeRoot.Start();
            }
            else
            {
                Logging.Write("Need to start HonorBuddy 1st");
                Lua.DoString("UIErrorsFrame:AddMessage(\"Err: Start HonorBuddy\", 1.0, 0.0, 0.0)");
            }
        }

        public static void SetKeyBind(System.Windows.Forms.Keys vKey)
        {
            _keyBind.KeyBind = vKey;
            SaveKeybind(vKey);
        }


        private static void SaveKeybind(System.Windows.Forms.Keys vKey)
        {
            HighVoltz.Properties.Settings.Default.Keybind = vKey;
            HighVoltz.Properties.Settings.Default.Save();
        }

        private static void loadSettings()
        {
            _keyBind.KeyBind = HighVoltz.Properties.Settings.Default.Keybind;
        }

    }
}

Mod:
I don't know if this works, but I simply just stuck the file in - Kick

 

Attachments

Last edited by a moderator:
Hey I use HB Toggle. How do I use this exactly?

EDIT: Thanks Kick, will be trying it out shortly.

EDIT2: Humm, doesn't appear to work for me. Maybe because I'ved tried it with HB 3895 only. Guess I'll try earlier versions 3804 and 3740.

EDIT3: I couldn't get it to work with 3804 or 3740 either.
 
Last edited:
Back
Top