Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to select my Global Hotkey from two drop down lists and then use the values in the RegisterHotKey() function to set the hotkey.

My code below gives an error saying the variables are the wrong data type.

I intend to feed in value selected from my modifier combobox and from my hotkey combobox once this error is corrected.

C#
string keyModifer = "(uint)fsModifiers.Control";
string keyCode = "(uint)Keys.Insert";
RegisterHotKey(_hWnd, 1, keyModifer, keyCode);


Thank you...
[EDIT - OP comment from "solution" (deleted)]
Quote:
know the data types for the function (I said I know my combobox strings are the wrong type).

I'm trying to avoid having to create a long switch statement for each hotkey combination. I'm wondering if there is a way to take my combobox Value and feed it into the function?

I'm not aware if a combobox Value can be of type 'fsModifiers' and of type 'keyCode'. I'm only aware of comboboxes having string and int data types for their value property (neither of which will work straight away on this RegisterHotKey().
[/EDIT]
Posted
Updated 26-Jun-14 12:21pm
v2
Comments
[no name] 26-Jun-14 17:42pm    
Since the parameters for RegisterHotKey are HWND, int, UINT and UINT, one has to wonder why you are passing strings to that function and then asking why it's not working.

1 solution

Hi,

You can create your own class

C#
public class HotKeyData
{
    public HotKeyData(string friendlyName, uint keyModifier, uint keyCode)
    {
        FriendlyName = friendlyName;
        KeyModifier = keyModifier;
        KeyCode = keyCode;
    }

    public string FriendlyName { get; set; }
    public uint KeyModifier { get; set; }
    public uint KeyCode { get; set; }
}


Then create a list with your class
C#
private List<HotKeyData> comboList = new List<HotKeyData>();


In e.g. Form_Load you add data to the list
C#
comboList.Add(new HotKeyData("Key1", 1, 11));
comboList.Add(new HotKeyData("Key2", 2, 12));
comboList.Add(new HotKeyData("Key3", 3, 13));
comboList.Add(new HotKeyData("Key4", 4, 14));


Then bind the list to the combobox
C#
comboBox1.DataSource = comboList;
comboBox1.DisplayMember = "FriendlyName";


Implement one event for the combo box.
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  HotKeyData keyData = (HotKeyData)comboBox1.SelectedItem;
  RegisterHotKey(_hWnd, 1, keyData.KeyModifier, keyData.KeyCode);
}


At least I think this is what you want.
 
Share this answer
 
Comments
rfresh 27-Jun-14 1:26am    
I'm getting an error on this line: The name 'RegisterHotKey' does not exist in the current content.

<pre lang="xml"><pre lang="c#">
RegisterHotKey(_hWnd, 1, keyData.KeyModifier, keyData.KeyCode);
</pre></pre>

Same for the _hWnd argument.

I tried to add:

<pre lang="c#">this._hWnd = hWnd;</pre>

but that also has an error.
George Jonsson 27-Jun-14 2:20am    
As you used RegisterHotKey in your initial question, I assumed that you already had a reference to that method.
rfresh 27-Jun-14 2:23am    
yes...i do...i need the using statement in the new class right?
George Jonsson 27-Jun-14 3:11am    
Well, I can't see your complete code.
The combo box event is normally implemented in your main form code.
Here you need to have a reference the method RegisterHotKey.
//George
rfresh 27-Jun-14 12:27pm    
I got it working...thanks for the help.
I use this code on form load:
thisWindow = FindWindow(null, "ChecklistForm");
UnregisterHotKey(thisWindow, 1);
HotKeyModifier keyDataModifier = (HotKeyModifier)cboHotKeyModifier.SelectedItem;
HotKey keyData = (HotKey)cboHotKey.SelectedItem;
RegisterHotKey(thisWindow, 1, keyDataModifier.KeyModifier, keyData.KeyCode);

and then I use the same code on the combobox SelectedIndexChanged() and then I try the new hotkey and it doesn't fire on the keyPressed.Msg == 0x0312 in WndProc().

I UnregisterHotKey() first and then I try to registerHotKey() again in the combobox SelectedIndexChanged event.

Is there something else I have to do when I use the combobox to change the hot key to get it to work again?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900