Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:10:23+00:00 2026-06-16T00:10:23+00:00

My Solution [DllImport(user32.dll)] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, int

  • 0

My Solution

[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, int vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public enum KeyModifiers : uint { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8, }
Actions<int, string> Directories = new Dictionary<int, string>();
const string MessageTitle = "Opps, Somthing Happened!";
const MessageBoxButtons msgButtons = MessageBoxButtons.OK;
const MessageBoxIcon msgIcon = MessageBoxIcon.Information;

private void btnCreateShortcut_Click(object sender, EventArgs e)
{
    if (cboModifier.SelectedIndex > 0)
    {
        uint key = (uint)Enum.Parse(typeof(KeyModifiers), cboModifier.SelectedItem.ToString());
        if (txtShortcutKey.Text != "")
            CreateHotKey(key, txtShortcutKey.Text.ToString());
        else
            MessageBox.Show("Please enter a Hot key to use", MessageTitle, msgButtons, msgIcon);
    }
    else
        MessageBox.Show("Please Select a Base Key", MessageTitle, msgButtons, msgIcon);
}

private void btnDestroyShortcuts_Click(object sender, EventArgs e)
{
    destroyShortcuts();
}

private void quickActions_FormClosing(object sender, FormClosingEventArgs e)
{
    destroyShortcuts();
}

protected override void WndProc(ref Message msg)
{
    switch (msg.Msg)
    {
        case 0x0312:
            if (Actions.ContainsKey((int)msg.WParam))
                // Preform Action
            break;
    }
    base.WndProc(ref msg);
}


public void destroyShortcuts()
{
    foreach (KeyValuePair<int, string> pair in Actions)
        UnregisterHotKey(this.Handle, pair.Key);

    lstActiveKeys.Items.Clear();
    Actions.Clear();
}

public void CreateHotKey(uint modifier, string key)
{
    int keyID = (Actions.Count + 1) * 100;
    Actions.Add(keyID, txtAction.Text.ToString());
    lstActiveKeys.Items.Add(modifier + "+" + key[0] + " - " + txtAction.Text.ToString());
    RegisterHotKey(this.Handle, keyID, modifier, (int)((char)key[0]));
}

I would like to know how to make it so that my users could define their own hot keys given the options for select a control key and a letter.

All of the code I found showed how to define one single hot key, but one users could have 3 and another could have 5 and they may not be the same keys.

What I would like, is given a control key and a alphanumeric key I can create a Windows Hot Key.

I also need to be able to destroy the registered keys when the application is closed.

PS: These needs to be system-wide not just within the application. Thanks @scott-chapman for pointing that out

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:10:24+00:00Added an answer on June 16, 2026 at 12:10 am

    Here is the solution I developed this is a C# method that appears to be working rather nicely.

    [DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, int vk);
    [DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    public enum KeyModifiers : uint { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8, }
    Actions<int, string> Directories = new Dictionary<int, string>();
    const string MessageTitle = "Opps, Somthing Happened!";
    const MessageBoxButtons msgButtons = MessageBoxButtons.OK;
    const MessageBoxIcon msgIcon = MessageBoxIcon.Information;
    
    private void btnCreateShortcut_Click(object sender, EventArgs e)
    {
        if (cboModifier.SelectedIndex > 0)
        {
            uint key = (uint)Enum.Parse(typeof(KeyModifiers), cboModifier.SelectedItem.ToString());
            if (txtShortcutKey.Text != "")
                CreateHotKey(key, txtShortcutKey.Text.ToString());
            else
                MessageBox.Show("Please enter a Hot key to use", MessageTitle, msgButtons, msgIcon);
        }
        else
            MessageBox.Show("Please Select a Base Key", MessageTitle, msgButtons, msgIcon);
    }
    
    private void btnDestroyShortcuts_Click(object sender, EventArgs e)
    {
        destroyShortcuts();
    }
    
    private void quickActions_FormClosing(object sender, FormClosingEventArgs e)
    {
        destroyShortcuts();
    }
    
    protected override void WndProc(ref Message msg)
    {
        switch (msg.Msg)
        {
            case 0x0312:
                if (Actions.ContainsKey((int)msg.WParam))
                    // Preform Action
                break;
        }
        base.WndProc(ref msg);
    }
    
    
    public void destroyShortcuts()
    {
        foreach (KeyValuePair<int, string> pair in Actions)
            UnregisterHotKey(this.Handle, pair.Key);
    
        lstActiveKeys.Items.Clear();
        Actions.Clear();
    }
    
    public void CreateHotKey(uint modifier, string key)
    {
        int keyID = (Actions.Count + 1) * 100;
        Actions.Add(keyID, txtAction.Text.ToString());
        lstActiveKeys.Items.Add(modifier + "+" + key[0] + " - " + txtAction.Text.ToString());
        RegisterHotKey(this.Handle, keyID, modifier, (int)((char)key[0]));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

below is the function from the winscard API [DllImport(winscard.dll)] public static extern int SCardTransmit
I wrote the easy test program by C# and .NET 4.0. [DllImport(user32.dll)] static extern
I've imported an API function like [DllImport(gdi32.dll)] private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc,
[DllImport(kernel32.dll)] private static extern Int32 AllocConsole(); I can open cmd.exe with this command. But
i have a method registered like this: [DllImport(MyApi.dll, CharSet = CharSet.Ansi)] private static extern
In my solution I've written the following: [DllImport(kernel32.dll, SetLastError = true, CharSet = CharSet.Auto)]
Im using a C++ Library into a C# solution through the [DllImport(C:\\gaul-windows.dll, ...)] attribute.
I have a solution that has a c++ dll project(MsgHook.cpp) with a function:- BOOL
public class ActiveWindow { public delegate void ActiveWindowChangedHandler(object sender, String windowHeader,IntPtr hwnd); public event
I am using DllImport in my solution. My problem is that I have two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.