简体   繁体   English

光标离屏时如何获取鼠标移动信息

[英]How to get mouse movement information when cursor is off-screen

[please see update at the end] 【更新见文末】

I'm an old stack overflow user (as most developers on, you know, Earth), but that's my first question here.我是一个老堆栈溢出用户(正如大多数开发人员一样,你知道,地球),但这是我在这里的第一个问题。

I'm trying to use an "air mouse" for gaming (pointing it to the screen), but as the mouse sensor is a gyroscope, there's some problems with off-screen movement that I'd like to try to fix by software.我正在尝试使用“空中鼠标”进行游戏(将其指向屏幕),但由于鼠标传感器是陀螺仪,因此我想尝试通过软件修复屏幕外移动的一些问题。

With this gyro mouse, when the user moves its arm pointing outside the screen, the cursor stops at the screen limit, which is no problem.使用这款陀螺鼠标,当用户将手臂指向屏幕外时,光标停在屏幕限制处,这没有问题。 However, when he moves his arm back, no matter the distance from the screen, the cursor immediately moves on-screen.然而,当他将手臂向后移动时,无论距离屏幕多远,光标都会立即在屏幕上移动。 This causes a giant difference between the air mouse real position and the cursor.这导致空中鼠标实际位置和光标之间存在巨大差异。

This could be fixed with a simple control over the number of pixels, and direction, travelled off-screen, in conjunction with some event handling.这可以通过对像素数量和方向的简单控制以及一些事件处理来解决。 If I could sum the number of "offscreen" pixels traveled in -X, +X, -Y and +Y, it would be possible to prevent/cancel the mouse move event - or set the cursor to its previous position, at the edge of the screen - until the control tells me that the physical mouse is pointing back to the screen.如果我可以对在 -X、+X、-Y 和 +Y 中移动的“屏幕外”像素的数量求和,则可以防止/取消鼠标移动事件 - 或者将光标设置到其先前的位置,在边缘屏幕 - 直到控件告诉我物理鼠标指向屏幕。 Just then I'd allow the cursor to move freely.就在那时,我会允许光标自由移动。

Maybe this isn't that usefull, but it's an interesting problem, and see this working would be fun as hell!也许这不是那么有用,但这是一个有趣的问题,看到这个工作会很有趣!

Currently, based on this great project , I can just state when the mouse is off-screen, but cannot control how far and in which direction it is moving, to properly implement what I'm trying.目前,基于这个伟大的项目,我只能说明鼠标何时离开屏幕,但无法控制它移动的距离和方向,以正确实现我正在尝试的内容。 It seems to me that such kind of information would be too low-level for my current Windows knowledge.在我看来,这种信息对于我目前的 Windows 知识来说太低级了。

So, to be clear: how can I, in C# (other languages accepted, but I'd have to learn a lot ;), get any kind of "delta position" information or direction of movement, when the cursor is at the limit of screen?所以,要清楚:当光标处于极限时,我如何在 C#(接受其他语言,但我必须学习很多;)中获得任何类型的“增量位置”信息或移动方向屏幕?

With all due respect, I'm not interested on using different kinds of controllers, as well as "you shouldn't do this" answers.恕我直言,我对使用不同类型的控制器以及“你不应该这样做”的回答不感兴趣。 I have this problem to solve, with these elements, and it would be great to make this work!我有这个问题要解决,有了这些元素,让这个工作变得很棒!

UPDATE:更新:

Ok, here's my progress up to now.好的,这是我到目前为止的进展。

In order to get raw mouse data and know about the mouse movement 'offscreen', I had to register my application, using some windows API functions, to receive WM_INPUT messages.为了获取原始鼠标数据并了解鼠标“屏幕外”移动,我必须使用一些 Windows API 函数注册我的应用程序以接收 WM_INPUT 消息。

Based on a very old code I found, I was able to get mouse raw data and implement exactly what I wanted.基于我发现的一个非常旧的代码,我能够获取鼠标原始数据并完全实现我想要的。 Except for the fact that this code is based on a WdnProc callback, and so it only works when my application has the focus.除了此代码基于 WdnProc 回调的事实,因此它仅在我的应用程序具有焦点时才有效。 And I need it to also work when the focus is elsewhere - after all, I'm trying to improve the pointing provided by a gyro mouse, for third party games.当焦点在其他地方时,我也需要它来工作 - 毕竟,我正在尝试改进陀螺仪鼠标提供的指向,用于第三方游戏。

It seems that I should use a hook ( a good example here ), but I have no idea how to hook for input messages.似乎我应该使用钩子( 这里是一个很好的例子),但我不知道如何钩住输入消息。 Tried to merge the code of the two links above, but the first one needs the message.LParam that is passed to the WdnProc - which seems to be unavailable when merely hooking mouse events.试图合并上面两个链接的代码,但第一个需要传递给 WdnProc 的 message.LParam - 这在仅挂钩鼠标事件时似乎不可用。

Now I'm way out of my league to make some real progress.现在我已经脱离了自己的联盟​​,可以取得一些真正的进步。 Any ideas?有任何想法吗?

One of the simplest solution to get cursor position and then detect its movement regardless where the cursor is to use win32 API from the user32.dll .获取光标位置然后检测其移动的最简单的解决方案之一,无论光标在哪里使用user32.dll win32 API。

Here is a simple code which gets the cursor position in every 10ms using the timer in the C# Windows Form application and displays it as the title of the window.这是一个简单的代码,它使用 C# Windows 窗体应用程序中的计时器每 10 毫秒获取一次光标位置,并将其显示为窗口的标题。

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;

public partial class Form1 : Form
{
    Timer timer = new Timer();

    public Form1()
    {
        InitializeComponent();

        timer.Interval = 10;
        timer.Tick += Timer_Tick;

        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        // do here whatever you want to do

        // just for testing...
        GetCursorPos(out Point lpPoint);
        this.Text = lpPoint.X + ", " + lpPoint.Y;
    }

    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out Point p);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM