tags:

views:

1639

answers:

4

I'm working on a project that needs to emulate a keypress of the Windows key. I've tried SendKeys.Send to no avail.

Specifically, the windows key needs to come in concert with a button. That is, I want to send Windows Key and plus / minus.

A: 

Try SendInput or the older keybd_event

Shay Erlichmen
+1  A: 

I think your best bet is using keybd_event keydown (called ExtendedKey) with the LWin value of the System.Windows.Forms.Keys enum, then keydown second character (+), and keyup both keys.

I do not believe SendKeys works with the Windows key as a modifier.

Alex B
+5  A: 

I would add that it is often unlikely for you to find lower level functions like these in the .NET framework. If you were confused as to why the suggestions both pointed to "non C#" functions, then you probably could use some details on P/Invoke.

Basically there are ways to define C# functions that "tie" them to Windows API functions that do not exist within .NET assemblies (Instead they are typically implemented in C++ and available as a standard DLL). This process is considered to be "(Windows) Platform Invoking" (thus P/Invoke).

It can be a bit wobbly at first to match up all the data types between C++ and C# style calls, but fortunately, there are others out there that have paved the way.

The suggested function, SendInput, has a PInvoke wrapper over at PInvoke.net. This wrapper class, when available in your assembly, will allow you to call SendInput as if it were a C# function.

PInvoke.net is basically a PInvoke wiki for well known API calls in windows, and typically has a C#/VB.NET wrapper of API calls.

ee
+1  A: 

This may be overkill, but you could try using AutoItX which is a way to use AutoIt as a DLL. I've only written standalone scripts, but I know AutoIt makes it very easy to simulate pressing the Windows key.

For example, to open the run dialog is just:

Send("#r") ;Win + R = run

Adam Neal