开发者

.NET - Detecting mouse movement through stubborn control

I am writing a C# Project that is relatively simple. Think "Public Web Terminal". Essentially there is a maximized form with a fill-docked web browser on it. The web browser control I am using is the WebKit Control found here:

WebKit Download Page

I am trying to detect system idle time by keeping a DateTime which represents the last time a mouse movement or key press was made.

I have instituted event handlers (see code below), and have come across a stumbling block. The mouse (and key) events seem to not fire when my mouse moves over the web document. It DOES work fine when my mouse touches the vertical scroll bar portion of the web browser control, so I know the code is OK - it seems to be some sort of "oversight" (for lack of a better word) on the control's par开发者_高级运维t.

I guess my question is - to all of you coders out there, how would you go about handling this?

this.webKitBrowser1.KeyPress += new KeyPressEventHandler(handleKeyPress);
this.webKitBrowser1.MouseMove += new MouseEventHandler(handleAction);
this.webKitBrowser1.MouseClick += new MouseEventHandler(handleAction);
this.webKitBrowser1.MouseDown += new MouseEventHandler(handleAction);
this.webKitBrowser1.MouseUp += new MouseEventHandler(handleAction);
this.webKitBrowser1.MouseDoubleClick += new MouseEventHandler(handleAction);

void handleKeyPress(object sender, KeyPressEventArgs e)
{
    this.handleAction(sender, null);
}

void handleAction(object sender, MouseEventArgs e)
{
    this.lastAction = DateTime.Now;
    this.label4.Text = this.lastAction.ToLongTimeString();
}

UPDATE

Using Joe's accepted solution, I put together the following class. Thanks to all who participated.

class classIdleTime
{
    [DllImport("user32.dll")]
    static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

    internal struct LASTINPUTINFO
    {
        public Int32 cbSize;
        public Int32 dwTime;
    }

    public int getIdleTime()
    {
        int systemUptime = Environment.TickCount;
        int LastInputTicks = 0;
        int IdleTicks = 0;

        LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
        LastInputInfo.cbSize = (Int32)Marshal.SizeOf(LastInputInfo);
        LastInputInfo.dwTime = 0;

        if (GetLastInputInfo(ref LastInputInfo))
        {
            LastInputTicks = (int)LastInputInfo.dwTime;
            IdleTicks = systemUptime - LastInputTicks;
        }
        Int32 seconds = IdleTicks / 1000;
        return seconds;
    }

USAGE

idleTimeObject = new classIdleTime();
Int32 seconds = idleTimeObject.getIdleTime();
this.isIdle = (seconds > secondsBeforeIdle);


You could just ask Windows if the user is idle. You'll have to use P/Invoke, but it would be the easiest. Check out the GetLastInputInfo function.


This looks like a WinForms app -- why not add an IMessageFilter? You'll get to see every Windows message that passes through the event loop, whether it's targetted at the browser or elsewhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜