C
C#2y ago
LikimiaD

❔ Recording hotkeys

How do I write down user hotkeys to save? For example, you want to use the combination CTRL+G, how do I save this command line? e.KeyDown shows the last key.
10 Replies
JimmahDean
JimmahDean2y ago
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int key);
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int key);
somewhere inside the namespace and then in your KeyPressed event, you want something like
if ((GetAsyncKeyState(keyNumber) & 0x8000) > 0)
{
System.Console.Write("Ctrl+");
}
System.Console.Write(e.KeyDown);
if ((GetAsyncKeyState(keyNumber) & 0x8000) > 0)
{
System.Console.Write("Ctrl+");
}
System.Console.Write(e.KeyDown);
where keyNumber is the identifier for Ctrl or shift of whatever full disclosure, I don't know why it needs the & 0x8000 this will also probably give you Ctrl+Ctrl but it's a start 🙂
Jester
Jester2y ago
you mean like a hotkey that whenever you press it, it runs even if your application is minimized? the problem with this is that you can miss it and have to keep looping
Jester
Jester2y ago
you can dll import this to create an actual hotkey maybe you can find a wrapper for it on github you receive a message in the message loop of your window if a registered hotkey is pressed
JimmahDean
JimmahDean2y ago
learn something new every day. Thanks 🙂
LikimiaD
LikimiaDOP2y ago
@Jester Thanks, mate. <a:revolving_hearts:970018936525578302> Can I import a DLL into C# and create an application so that it works without the DLL(from exe) or I still have to substitute it?
Jester
Jester2y ago
that function is in the user32 dll which is in system32 so every windows pc has it. you domt have to put it in your application or anything
Jester
Jester2y ago
DllImportAttribute Class (System.Runtime.InteropServices)
Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point.
Jester
Jester2y ago
this way you load the dll at runtime to be able to call the function, thats how things are done
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server