1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| using System;
using System.Runtime.InteropServices;
using Microsoft.DirectX.DirectInput;
namespace monProjet
{
public static class PressEnter
{
[DllImport("user32.dll")]
public static extern int MapVirtualKey(uint uCode, uint uMapType);
[StructLayout(LayoutKind.Sequential)]
private struct INPUT
{
public UInt32 type;
public ushort wVk;
public ushort wScan;
public UInt32 dwFlags;
public UInt32 time;
public UIntPtr dwExtraInfo;
public UInt32 uMsg;
public ushort wParamL;
public ushort wParamH;
}
private enum SendInputFlags
{
KEYEVENTF_EXTENDEDKEY = 0x0001,
KEYEVENTF_KEYUP = 0x0002,
KEYEVENTF_UNICODE = 0x0004,
KEYEVENTF_SCANCODE = 0x0008,
}
[DllImport("user32.dll")]
private static extern UInt32 SendInput(UInt32 nInputs,
[MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs,
Int32 cbSize);
public static void Activate()
{
INPUT[] InputData = new INPUT[1];
InputData[0].type = 1;
InputData[0].wScan = (ushort)Key.A;
InputData[0].dwFlags = (uint)SendInputFlags.KEYEVENTF_SCANCODE;
InputData[0].time = 0;
InputData[0].dwExtraInfo = UIntPtr.Zero;
// send keydown
if (SendInput(1, InputData, Marshal.SizeOf(InputData[0])) == 0)
{
System.Windows.Forms.MessageBox.Show("Erreur lors de l'envoie de la touche - Raison : " +
Marshal.GetLastWin32Error().ToString());
}
}
}
} |
Partager