key kk
=
new
key();
// IntPtr a = new IntPtr();
kk.sendwinio();
kk.MykeyDown(( int )key.VirtualKeys.VK_F1);
System.Threading.Thread.Sleep( 2000 );
kk.MykeyUp(( int )key.VirtualKeys.VK_F1);
这是模拟键盘的调用方式
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Management;
namespace sendkey
... {
public class key
...{
const uint PROCESS_ALL_ACCESS = 0x001F0FFF;
const uint KEYEVENTF_EXTENDEDKEY = 0x1;
const uint KEYEVENTF_KEYUP = 0x2;
private readonly int MOUSEEVENTF_LEFTDOWN = 0x2;
private readonly int MOUSEEVENTF_LEFTUP = 0x4;
const uint KBC_KEY_CMD = 0x64;
const uint KBC_KEY_DATA = 0x60;
//得到窗体句柄的函数,FindWindow函数用来返回符合指定的类名( ClassName )和窗口名( WindowTitle )的窗口句柄
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(
string lpClassName, // pointer to class name
string lpWindowName // pointer to window name
);
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr id,int pid);
[DllImport("kernel32.dll")]
private static extern void CloseHandle
(
uint hObject //Handle to object
);
//读取进程内存的函数
[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory(uint hProcess, IntPtr lpBaseAddress,
IntPtr lpBuffer, uint nSize, ref uint lpNumberOfBytesRead);
//得到目标进程句柄的函数
[DllImport("kernel32.dll")]
public static extern uint OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
//鼠标事件声明
[DllImport("user32.dll")]
static extern bool setcursorpos(int x, int y);
[DllImport("user32.dll")]
static extern void mouse_event(mouseeventflag flags, int dx, int dy, uint data, UIntPtr extrainfo);
//键盘事件声明
[DllImport("user32.dll")]
static extern byte MapVirtualKey(byte wCode, int wMap);
[DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);
[DllImport("user32.dll")]
static extern void keybd_event( byte bVk, byte bScan,uint dwFlags,uint dwExtraInfo);
//键盘事件声明winio
[DllImport("winio.dll")]
public static extern bool InitializeWinIo();
[DllImport("winio.dll")]
public static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize);
[DllImport("winio.dll")]
public static extern bool SetPortVal(uint wPortAddr, IntPtr dwPortVal, byte bSize);
[DllImport("winio.dll")]
public static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle);
[DllImport("winio.dll")]
public static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr);
[DllImport("winio.dll")]
public static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal);
[DllImport("winio.dll")]
public static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal);
[DllImport("winio.dll")]
public static extern void ShutdownWinIo();
/**//// <summary>
/// 获取进程pid
/// </summary>
// IntPtr a = new IntPtr();
kk.sendwinio();
kk.MykeyDown(( int )key.VirtualKeys.VK_F1);
System.Threading.Thread.Sleep( 2000 );
kk.MykeyUp(( int )key.VirtualKeys.VK_F1);
这是模拟键盘的调用方式
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Management;
namespace sendkey
... {
public class key
...{
const uint PROCESS_ALL_ACCESS = 0x001F0FFF;
const uint KEYEVENTF_EXTENDEDKEY = 0x1;
const uint KEYEVENTF_KEYUP = 0x2;
private readonly int MOUSEEVENTF_LEFTDOWN = 0x2;
private readonly int MOUSEEVENTF_LEFTUP = 0x4;
const uint KBC_KEY_CMD = 0x64;
const uint KBC_KEY_DATA = 0x60;
//得到窗体句柄的函数,FindWindow函数用来返回符合指定的类名( ClassName )和窗口名( WindowTitle )的窗口句柄
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(
string lpClassName, // pointer to class name
string lpWindowName // pointer to window name
);
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr id,int pid);
[DllImport("kernel32.dll")]
private static extern void CloseHandle
(
uint hObject //Handle to object
);
//读取进程内存的函数
[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory(uint hProcess, IntPtr lpBaseAddress,
IntPtr lpBuffer, uint nSize, ref uint lpNumberOfBytesRead);
//得到目标进程句柄的函数
[DllImport("kernel32.dll")]
public static extern uint OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
//鼠标事件声明
[DllImport("user32.dll")]
static extern bool setcursorpos(int x, int y);
[DllImport("user32.dll")]
static extern void mouse_event(mouseeventflag flags, int dx, int dy, uint data, UIntPtr extrainfo);
//键盘事件声明
[DllImport("user32.dll")]
static extern byte MapVirtualKey(byte wCode, int wMap);
[DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);
[DllImport("user32.dll")]
static extern void keybd_event( byte bVk, byte bScan,uint dwFlags,uint dwExtraInfo);
//键盘事件声明winio
[DllImport("winio.dll")]
public static extern bool InitializeWinIo();
[DllImport("winio.dll")]
public static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize);
[DllImport("winio.dll")]
public static extern bool SetPortVal(uint wPortAddr, IntPtr dwPortVal, byte bSize);
[DllImport("winio.dll")]
public static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle);
[DllImport("winio.dll")]
public static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr);
[DllImport("winio.dll")]
public static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal);
[DllImport("winio.dll")]
public static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal);
[DllImport("winio.dll")]
public static extern void ShutdownWinIo();
/**//// <summary>
/// 获取进程pid
/// </summary>