TolikPylypchuk / SharpHook

SharpHook provides a cross-platform global keyboard and mouse hook, event simulation, and text entry simulation for .NET
https://sharphook.tolik.io
MIT License
347 stars 32 forks source link

Is it possible to emulate pressing the Windows Key (winkey or start key) #75

Closed brightertools closed 11 months ago

brightertools commented 11 months ago

I am trying to emulate pressing the windows + . (dot) to get the Windows Emoji Keyboard.

I just cannot seem to see it..

also the command key on MacOS, to do the same thing with Command + Control + Space

brightertools commented 11 months ago

I found the code:

var simulator = new EventSimulator(); simulator.SimulateKeyPress(SharpHook.Native.KeyCode.VcLeftMeta); simulator.SimulateKeyPress(SharpHook.Native.KeyCode.VcPeriod);

This does open the emoji window on windows (I do seem to need to set the focus on the test box) and its seems to work however when I close the box my computer stil thinks I am pressing the windows key

brightertools commented 11 months ago

ok got it, sorry

The actions are press then release, so this works

var simulator = new EventSimulator(); simulator.SimulateKeyPress(KeyCode.VcLeftMeta); simulator.SimulateKeyPress(KeyCode.VcPeriod); simulator.SimulateKeyRelease(KeyCode.VcLeftMeta); simulator.SimulateKeyRelease(KeyCode.VcPeriod);

TolikPylypchuk commented 11 months ago

Hi! Thanks for posting this question! And I'm glad you've figured it out before I even saw the question :)

Yes, the VcLeftMeta and VcRightMeta key codes represent different keys on different platforms:

And yes, you need to explicitly simulate pressing and the releasing keys. I would recommend releasing the keys in the reverse order as I think it's the way we do that naturally - for example, when pressing Ctrl+C, you press Ctrl, then press C, then release C, and finally, release Ctrl. But if it works, you can leave it either way.

Also, here's the list of key code mappings for each platform.