Results 1 to 4 of 4
  1. #1
    Lutherion's Avatar
    Join Date
    Nov 2017
    Gender
    male
    Posts
    9
    Reputation
    10
    Thanks
    0

    Application.Exit

    Hello,
    I use c# form. And what I have for now is a button down event and in that I have a if statement for the P key, and when I press this key. It closes the application, but it only closes when I have the form selected, how can you make it so, that when your in-game in maybe cs-go, you can close the c# form?

  2. #2
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    At the top of your class:
    Code:
    [DllImport("user32.dll")] 
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    
    const int EXIT_HOTKEY_ID = 1;
    In your mainconstructor:
    Code:
    // The 6 is a modifier keycode: Alt = 1, Ctrl = 2, Shift = 4, Win = 8
    // So 6 is ctrl+shift
    RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 6, (int) Keys.F4);
    Somewhere in your main form
    Code:
    protected override void WndProc(ref Message m) 
    {
        if (m.Msg == 0x0312 && m.WParam.ToInt32() == EXIT_HOTKEY_ID) 
        {
            // Your hotkey was pressed
        }
        base.WndProc(ref m);
    }
    Don't forget to unregister your hotkey on exit.
    Ah we-a blaze the fyah, make it bun dem!

  3. The Following User Says Thank You to Hell_Demon For This Useful Post:

    Lutherion (01-15-2018)

  4. #3
    lemdpcfa's Avatar
    Join Date
    Aug 2012
    Gender
    male
    Location
    Paris
    Posts
    12
    Reputation
    10
    Thanks
    0
    this is easy. don't need to unregister it.

    Code:
    using System;
    using System.Collections;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Forms;
    using System.Windows.Interop;
    
    namespace class
    {
        public class HotKey
        {
            private static Hashtable KeyPair = new Hashtable();
            private IntPtr Handle;
            private Window window;
            private uint Controlkey;
            private uint Key;
            private const int WM_HOTKEY = 786;
    
            public int KeyId { set; get; }
    
            public event HotKey.OnHotkeyEventHandeler OnHotKey;
    
            public void Regist(Window win, HotKey.KeyFlags control, Keys key)
            {
                Handle = new WindowInteropHelper(win).Handle;
                window = win;
                Controlkey = (uint)control;
                Key = (uint)key;
                KeyId = (int)Controlkey + (int)Key * 10;
                if (HotKey.KeyPair.ContainsKey((object)KeyId))
                    throw new Exception("Error happened HOTKEY 0x12");
                if (!HotKey.RegisterHotKey(Handle, KeyId, Controlkey, Key))
                    throw new Exception("Error happened HOTKEY 0x13");
                if (HotKey.KeyPair.Count == 0 && !HotKey.InstallHotKeyHook(this))
                    throw new Exception("Error happened HOTKEY 0x12");
                HotKey.KeyPair.Add((object)KeyId, (object)this);
            }
    
            public void UnRegist(Window win, int KeyId)
            {
                HotKey.UnregisterHotKey(new WindowInteropHelper(win).Handle, KeyId);
            }
    
            [DllImport("user32")]
            private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint controlKey, uint virtualKey);
    
            [DllImport("user32")]
            private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    
            private static bool InstallHotKeyHook(HotKey hk)
            {
                if (hk.window == null || hk.Handle == IntPtr.Zero)
                    return false;
                HwndSource hwndSource = HwndSource.FromHwnd(hk.Handle);
                if (hwndSource == null)
                    return false;
                hwndSource.AddHook(new HwndSourceHook(HotKey.HotKeyHook));
                return true;
            }
    
            private static IntPtr HotKeyHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                if (msg == 786)
                {
                    HotKey hotKey = (HotKey)HotKey.KeyPair[(object)(int)wParam];
                    // ISSUE: reference to a compiler-generated field
                    if (hotKey.OnHotKey != null)
                    {
                        // ISSUE: reference to a compiler-generated field
                        hotKey.OnHotKey();
                    }
                }
                return IntPtr.Zero;
            }
    
            ~HotKey()
            {
                HotKey.UnregisterHotKey(this.Handle, this.KeyId);
            }
    
            public delegate void OnHotkeyEventHandeler();
    
            public enum KeyFlags
            {
                Mod_None = 0,
                MOD_ALT = 1,
                MOD_CONTROL = 2,
                MOD_CONTROLALT = 3,
                MOD_SHIFT = 4,
                MOD_WIN = 8,
            }
        }
    }

    use it like that on form loaded():
    Code:
                try {
                    HotKey hotKey = new HotKey();
                    hotKey.Regist((Window)this, HotKey.KeyFlags.Mod_None, Keys.F9);
                    hotKey.OnHotKey += new HotKey.OnHotkeyEventHandeler(your_function);
                } catch { //error }

  5. #4
    shark17's Avatar
    Join Date
    Nov 2019
    Gender
    male
    Posts
    6
    Reputation
    10
    Thanks
    0
    Thanks for the useful information

Similar Threads

  1. [Application] Undetected Module Maker V1.0
    By radnomguywfq3 in forum Visual Basic Programming
    Replies: 72
    Last Post: 12-31-2009, 06:50 AM
  2. WarRock Mod Applications[DISCUSSION]
    By BPK in forum General
    Replies: 16
    Last Post: 12-22-2007, 07:25 PM
  3. how can i make Exit button
    By sukh13 in forum WarRock - International Hacks
    Replies: 9
    Last Post: 10-21-2007, 02:18 AM
  4. Exit Fuckface Enter Outzida
    By OutZida in forum Art & Graphic Design
    Replies: 8
    Last Post: 02-08-2006, 11:10 AM
  5. Not a valid win32 application
    By terence in forum General Game Hacking
    Replies: 1
    Last Post: 02-03-2006, 06:52 AM