Search
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» ~=?{The future is here, TastyExploit officially open to the public}?=~
Tue Mar 02, 2010 11:00 pm by Dami

» My first aa script
Tue Mar 02, 2010 7:00 am by wafflemaster

» the real story behind Maverin
Tue Mar 02, 2010 1:33 am by SoundOfDeat

» Random spam
Mon Mar 01, 2010 11:52 pm by Danny1994

» [Request] EMS v56 Exe
Mon Mar 01, 2010 12:39 am by Dami

» [Blacklist] NX-Trading Blacklist.
Mon Mar 01, 2010 12:38 am by Danny1994

» I have a question regarding the meso farmer
Sat Feb 27, 2010 10:30 pm by Dami

» What are you listening to now?
Sat Feb 27, 2010 7:57 pm by Noname111111

» Video(s) Of The Day
Sat Feb 27, 2010 7:37 pm by Noname111111

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search
Affiliates
free forum
 



[C#] User Customizable Hotkeys with a Hook

Go down

[C#] User Customizable Hotkeys with a Hook

Post by Dami on Fri Oct 30, 2009 3:46 am

By samuri25404

This post is going to discuss how to use the keyboard hook that I created with user-customizable hotkeys.

First you'll need my keyboard hook class (go get it from the other post if you don't have it already).

Next, we're going to create an interface, for incredible simplicity:

Code:

    public interface IKeyFunction
    {
        string classname
        {
            get;
        }
        Hook.VK key
        {
            get;
            set;
        }
 
        void Operation();
    }

If you don't know about interfaces, I suggest you Google.

Now, we should define some classes that inherit from this interface. I'll show an example:

Code:
    public class Godmode : IKeyFunction
    {
        public string classname
        { get { return "Godmode"; } }
 
        private Hook.VK _key; //we use this to prevent an infinite loop
 
        public Hook.VK key
        {
            get { return _key; }
            set { _key = value; }
        }
 
        public void Operation()
        {
 
        }
    }

And you would simply put whatever it's supposed to do in the Operation() method. (Forget about the "classname" property for now)

~~

After defining all the classes that you want, create this:
Code:

 public static List<IKeyFunction> keyfunctions = new List<IKeyFunction>(
            new IKeyFunction[] {
                new Godmode(),
            });

And here, I'm just using the Godmode class as an example. Replace the "Godmode" with whatever your classes name is. Then, simply create as many of those as you need.

~~

Now that we've got all of our events set up, we need to create our KeyHandler() method.

Start off with the bare backbone:
Code:

        public static void KeyHandler(
            IntPtr wParam,
            IntPtr lParam)
        { 
           
        }

Then, add this code into it:
Code:

            //first get the key that we need
 
            int key = System.Runtime.InteropServices.Marshal.ReadInt32(lParam);
 
            Hook.VK vk = (Hook.VK)key;

This is nothing new--simply getting the key.

However, here's where the new stuff comes in:
Code:

            //loop through all of the classes to find a match on the hotkey
 
            IKeyFunction kf = null;           
 
            foreach (IKeyFunction keyfunction in keyfunctions)
            {
                if (keyfunction.key == vk)
                {
                    kf = keyfunction;
                    break;
                }
            }
 
            if (kf == null) //there was no match, so we don't need to do anything
                return;
 
            kf.Operation();

~~

Now, for editing the hotkeys of the little classes...

Remember that little "classname" property that we had in the interface?
Code:

        public static void RegisterHotkey(
            string sClass,
            Hook.VK key)
        {
            IKeyFunction kf = null, temp;
 
            foreach (IKeyFunction keyfunction in keyfunctions)
            {
                if (keyfunction.classname == sClass)
                {
                    kf = keyfunction;
                    break;
                }
            }
 
            if (kf == null) return;
 
            temp = kf;
 
            //now we've got the keyfunction
 
            //set our property
 
            kf.key = key;
 
            //reupdate the list:
 
            //first clear away the old class
 
            keyfunctions.Remove(temp);
 
            //now add the new one
 
            keyfunctions.Add(kf);
        }

I've commented it enough that you should understand it, but if you don't, my Inbox is always open.

Have fun!
avatar
Dami
Dragon
Dragon

Posts : 1414
Join date : 2009-08-10
Age : 29
Location : Finland

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum