请问一下:
本人在做一个小项目:现有一支蓝牙笔(笔上有一个按键)以及一个显示屏。
功能是:使蓝牙笔能当左右鼠标使用。 现遇到一个问题,现通过按键能单独实现左,右键;但当右键按下后,一直为右键菜单,不能响应左键事件。
代码如下: //for firing mouse and keyboard events
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); /declare consts for mouse messages
public const int INPUT_MOUSE = 0;
public const int INPUT_KEYBOARD = 1;
public const int INPUT_HARDWARE = 2; public const int MOUSEEVENTF_MOVE = 0x01;
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
public const int MOUSEEVENTF_MIDDLEUP = 0x40;
public const int MOUSEEVENTF_ABSOLUTE = 0x8000; [StructLayout(LayoutKind.Explicit, Size = 28)]
public struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)] //*
public MOUSEINPUT mi;
[FieldOffset(4)] //*
public KEYBDINPUT ki;
[FieldOffset(4)] //*
public HARDWAREINPUT hi;
}
//imports mouse_event function from user32.dll [DllImport("user32.dll")]
private static extern void mouse_event(
long dwFlags, // motion and click options
long dx, // horizontal position or change
long dy, // vertical position or change
long dwData, // wheel movement
long dwExtraInfo // application-defined information
); [DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetCursorPos(int X, int Y);
void OnTimedEvent(object source, WiimoteChangedEventArgs args)
{
mut2.WaitOne(); aTimer.Start(); // 开始计时
WiimoteState ws = args.WiimoteState; if (!lastWiiState.IRState.Found1)
{
lastWiiState.IRState.Found1 = ws.IRState.Found1;
// smoothingBufferIndex = 0;//resets the count if (cursorControl)
{
INPUT[] buffer = new INPUT[1]; // right down
buffer[0].type = INPUT_MOUSE;
buffer[0].mi.dx = 0; //(;int)(warpedX * 65535.0f / screenWidth);
buffer[0].mi.dy = 0; //(int)(warpedY * 65535.0f / screenHeight);
buffer[0].mi.mouseData = 0;
buffer[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
buffer[0].mi.time = 0;
buffer[0].mi.dwExtraInfo = (IntPtr)0; /////////////////////
// left down
/* buffer[1].type = INPUT_MOUSE;
buffer[1].mi.dx = 0;
buffer[1].mi.dy = 0;
buffer[1].mi.mouseData = 0;
buffer[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
buffer[1].mi.time = 0;
buffer[1].mi.dwExtraInfo = (IntPtr)0;
*/
SendInput(1, buffer, Marshal.SizeOf(buffer[0]));
}
} else
{
aTimer.Stop(); // 计时结束 lastWiiState.IRState.Found1 = ws.IRState.Found1; if (cursorControl)
{ INPUT[] buffer = new INPUT[1]; // 2 ->3 // 右键
buffer[0].type = INPUT_MOUSE;
buffer[0].mi.dx = 0;
buffer[0].mi.dy = 0;
buffer[0].mi.mouseData = 0;
buffer[0].mi.time = 0;
buffer[0].mi.dwExtraInfo = (IntPtr)0; // 移动
//buffer[1].type = INPUT_MOUSE;
//buffer[1].mi.dx = 0;
//buffer[1].mi.dy = 0;
//buffer[1].mi.mouseData = 0;
//buffer[1].mi.dwFlags = MOUSEEVENTF_MOVE;
//buffer[1].mi.time = 0;
//buffer[1].mi.dwExtraInfo = (IntPtr)0; SendInput(1, buffer, Marshal.SizeOf(buffer[0]));
}
} INPUT[] buffer1 = new INPUT[1]; // 2 ->3 // 左键
buffer1[0].type = INPUT_MOUSE;
buffer1[0].mi.dx = 0;
buffer1[0].mi.dy = 0;
buffer1[0].mi.mouseData = 0;
buffer1[0].mi.time = 0; {
// MessageBox.Show("Right");
buffer1[0].mi.dwFlags = MOUSEEVENTF_LEFTUP;
}
buffer1[0].mi.dwExtraInfo = (IntPtr)0;
SendInput(1, buffer1, Marshal.SizeOf(buffer1[0]));
mut2.ReleaseMutex(); ;
}
请问高手指点,谢谢啦!
本人在做一个小项目:现有一支蓝牙笔(笔上有一个按键)以及一个显示屏。
功能是:使蓝牙笔能当左右鼠标使用。 现遇到一个问题,现通过按键能单独实现左,右键;但当右键按下后,一直为右键菜单,不能响应左键事件。
代码如下: //for firing mouse and keyboard events
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); /declare consts for mouse messages
public const int INPUT_MOUSE = 0;
public const int INPUT_KEYBOARD = 1;
public const int INPUT_HARDWARE = 2; public const int MOUSEEVENTF_MOVE = 0x01;
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
public const int MOUSEEVENTF_MIDDLEUP = 0x40;
public const int MOUSEEVENTF_ABSOLUTE = 0x8000; [StructLayout(LayoutKind.Explicit, Size = 28)]
public struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)] //*
public MOUSEINPUT mi;
[FieldOffset(4)] //*
public KEYBDINPUT ki;
[FieldOffset(4)] //*
public HARDWAREINPUT hi;
}
//imports mouse_event function from user32.dll [DllImport("user32.dll")]
private static extern void mouse_event(
long dwFlags, // motion and click options
long dx, // horizontal position or change
long dy, // vertical position or change
long dwData, // wheel movement
long dwExtraInfo // application-defined information
); [DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetCursorPos(int X, int Y);
void OnTimedEvent(object source, WiimoteChangedEventArgs args)
{
mut2.WaitOne(); aTimer.Start(); // 开始计时
WiimoteState ws = args.WiimoteState; if (!lastWiiState.IRState.Found1)
{
lastWiiState.IRState.Found1 = ws.IRState.Found1;
// smoothingBufferIndex = 0;//resets the count if (cursorControl)
{
INPUT[] buffer = new INPUT[1]; // right down
buffer[0].type = INPUT_MOUSE;
buffer[0].mi.dx = 0; //(;int)(warpedX * 65535.0f / screenWidth);
buffer[0].mi.dy = 0; //(int)(warpedY * 65535.0f / screenHeight);
buffer[0].mi.mouseData = 0;
buffer[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
buffer[0].mi.time = 0;
buffer[0].mi.dwExtraInfo = (IntPtr)0; /////////////////////
// left down
/* buffer[1].type = INPUT_MOUSE;
buffer[1].mi.dx = 0;
buffer[1].mi.dy = 0;
buffer[1].mi.mouseData = 0;
buffer[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
buffer[1].mi.time = 0;
buffer[1].mi.dwExtraInfo = (IntPtr)0;
*/
SendInput(1, buffer, Marshal.SizeOf(buffer[0]));
}
} else
{
aTimer.Stop(); // 计时结束 lastWiiState.IRState.Found1 = ws.IRState.Found1; if (cursorControl)
{ INPUT[] buffer = new INPUT[1]; // 2 ->3 // 右键
buffer[0].type = INPUT_MOUSE;
buffer[0].mi.dx = 0;
buffer[0].mi.dy = 0;
buffer[0].mi.mouseData = 0;
buffer[0].mi.time = 0;
buffer[0].mi.dwExtraInfo = (IntPtr)0; // 移动
//buffer[1].type = INPUT_MOUSE;
//buffer[1].mi.dx = 0;
//buffer[1].mi.dy = 0;
//buffer[1].mi.mouseData = 0;
//buffer[1].mi.dwFlags = MOUSEEVENTF_MOVE;
//buffer[1].mi.time = 0;
//buffer[1].mi.dwExtraInfo = (IntPtr)0; SendInput(1, buffer, Marshal.SizeOf(buffer[0]));
}
} INPUT[] buffer1 = new INPUT[1]; // 2 ->3 // 左键
buffer1[0].type = INPUT_MOUSE;
buffer1[0].mi.dx = 0;
buffer1[0].mi.dy = 0;
buffer1[0].mi.mouseData = 0;
buffer1[0].mi.time = 0; {
// MessageBox.Show("Right");
buffer1[0].mi.dwFlags = MOUSEEVENTF_LEFTUP;
}
buffer1[0].mi.dwExtraInfo = (IntPtr)0;
SendInput(1, buffer1, Marshal.SizeOf(buffer1[0]));
mut2.ReleaseMutex(); ;
}
请问高手指点,谢谢啦!
你好像吧原来定义为左键操作改成了右键,但这里好像没改掉
buffer1[0].type = INPUT_MOUSE;
buffer1[0].mi.dx = 0;
buffer1[0].mi.dy = 0;
buffer1[0].mi.mouseData = 0;
buffer1[0].mi.time = 0; {
// MessageBox.Show("Right");
buffer1[0].mi.dwFlags = MOUSEEVENTF_RIGHTUP; }
buffer1[0].mi.dwExtraInfo = (IntPtr)0;
SendInput(1, buffer1, Marshal.SizeOf(buffer1[0]));