C#使用arduino +传感器控制鼠标 [英] C# controlling mouse using arduino + sensor

查看:224
本文介绍了C#使用arduino +传感器控制鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我的程序设法从传感器接收值并将其放入变量中.现在,我想使用该数据来控制鼠标的移动.对于鼠标位置,该变量包含x1和y1,单击是0 =无,1 =左键单击,2 =右键单击.我试图修改Johnny Lee wiiWhiteboard源代码,其中删除了任何wii lib引用,并从这些变量设置了输入..但是没有用.到目前为止,这是我的代码.

So far, my program manage to receive values from my sensor and put it in variables. now i want to control mouse movement using that data. the variable consist x1 and y1 for mouse position, click which is 0 = none, 1 = left click, 2 = right click. I''ve tried to modified Johnny Lee wiiWhiteboard Source Code where removing any wii lib referance and set the input from those variable.. but didn''t works. here''s my code so far..

public void mouseMovement()
        {
            mut.WaitOne();

            if (spot !=0)
            {
                int x = x1;
                int y = y1;
                float warpedX = x;
                float warpedY = y;
                warper.warp(x, y, ref warpedX, ref warpedY);

                if (spot == 1)//mouse down
                {
                
                    if (cursorControl)
                    {
                        INPUT[] buffer = new INPUT[2];
                        buffer[0].type = INPUT_MOUSE;
                        buffer[0].mi.dx = (int)(warpedX *65535.0f/screenWidth);
                        buffer[0].mi.dy = (int)(warpedY * 65535.0f / screenHeight);
                        buffer[0].mi.mouseData = 0;
                        buffer[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
                        buffer[0].mi.time = 0;
                        buffer[0].mi.dwExtraInfo = (IntPtr)0;

                        SendInput(2, buffer, Marshal.SizeOf(buffer[0]));

                    }



我虽然这行将足以移动指针..但没有..什么都没有发生.请帮忙,对不起,英语不好.感谢



i though this line will enough to move the pointer.. but no.. nothing happens. please help and sorry for bad english. thanks

推荐答案

使用特定于平台的功能总是很不好的.此外,我看不到您的代码如何工作.请参阅我对这个问题的评论.

做这种事情的一种方法是:您可以使用类System.Windows.Forms.CursorSystem.Windows.Forms来移动鼠标.请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.forms.cursor.aspx [ ^ ].



基本用法:

It''s always bad to use platform-specific features. Besides, I cannot see how your code could work. Please see my comments to the question.

One way of doing such things is this: you could move mouse with System.Windows.Forms using the class System.Windows.Forms.Cursor. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.aspx[^].



Basic usage:

int newX = //...
int newY = //...
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(newX, newY);



请参阅:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx [ ^ ].

但是,请通过其文档学习整个课程.在学习图书馆或作为开发的一部分进行研究时,务必做到这一点.

—SA



Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx[^].

However, study the whole class by its documentation. Always do it while learning a library or doing research as part of development.

—SA


如果我了解您,则想控制鼠标单击.在c#中,您可以使用以下代码来控制Windows上的鼠标单击:
If I understood you, you want to control your mouse clicks. In c# you can use the following code to control mouse clicks on Windows:
using System.Runtime.InteropServices;

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

public void DoaClick(int a)
        {
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            if (a==1)
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToUInt32(X), Convert.ToUInt32(Y), Convert.ToUInt32(0), Convert.ToUInt32(0));
            else if (a==2)
                mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, Convert.ToUInt32(X), Convert.ToUInt32(Y), Convert.ToUInt32(0), Convert.ToUInt32(0));
        }


这篇关于C#使用arduino +传感器控制鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆