spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Check if non-modifier keys are in pressed state during mouse events


You have several options:

Option 1: Add custom message filter to your application:

private KeyMessageFilter _filter = new
KeyMessageFilter();

private void Form1_Load(object sender, EventArgs
e)
{
    Application.AddMessageFilter(_filter);    
}    

public class KeyMessageFilter : IMessageFilter
{
    private Dictionary<Keys, bool> _keyTable
= new Dictionary<Keys, bool>();

    public Dictionary<Keys, bool> KeyTable
    {
        get { return _keyTable; }
        private set { _keyTable = value; }
    }

    public bool IsKeyPressed()
    {
        return _keyPressed; 
    }

    public bool IsKeyPressed(Keys k)
    {
        bool pressed = false;

        if (KeyTable.TryGetValue(k, out pressed))
        {
            return pressed;                  
        }

        return false; 
    }

    private const int WM_KEYDOWN = 0x0100;

    private const int WM_KEYUP = 0x0101;

    private bool _keyPressed = false;


    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_KEYDOWN)
        {
            KeyTable[(Keys)m.WParam] = true;

            _keyPressed = true;
        }

        if (m.Msg == WM_KEYUP)
        {                
            KeyTable[(Keys)m.WParam] = false;

            _keyPressed = false;
        }

        return false;        
}

Option 2: Use GetKeyState API function as linked before:

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

namespace MouseKeyboardStateTest
{
  public abstract class Keyboard
  {
    [Flags]
    private enum KeyStates
    {
      None = 0,
      Down = 1,
      Toggled = 2
    }

    [DllImport("user32.dll", CharSet =
CharSet.Auto, ExactSpelling = true)]
    private static extern short GetKeyState(int
keyCode);

    private static KeyStates GetKeyState(Keys key)
    {
      KeyStates state = KeyStates.None;

      short retVal = GetKeyState((int)key);

      //If the high-order bit is 1, the key is
down
      //otherwise, it is up.
      if ((retVal & 0x8000) == 0x8000)
        state |= KeyStates.Down;

      //If the low-order bit is 1, the key is
toggled.
      if ((retVal & 1) == 1)
        state |= KeyStates.Toggled;

      return state;
    }

    public static bool IsKeyDown(Keys key)
    { 
      return KeyStates.Down == (GetKeyState(key)
& KeyStates.Down);
    }

    public static bool IsKeyToggled(Keys key)
    { 
      return KeyStates.Toggled ==
(GetKeyState(key) & KeyStates.Toggled);
    }
  }
}

Option 3: Handle KeyDown And KeyUp events by yourself. It's not best approach but for small application would be nice.


Categories : C#

Related to : Check if non-modifier keys are in pressed state during mouse events
Python [Canvas]: Detect Mouse Button NOT pressed
Detect mouse press and release events. When one of those events occur, start or stop saving as appropriate. canvas.bind("<Button-1>", start_saving) canvas.bind("<ButtonRelease-1>", stop_saving)

Categories : Python
Monitor the time between mouse events with JavaScript
Something like this might help var timer; function tsMouseDown() { timer = setTimeout( function(){ div.textContent = "tsMouseDown ... 500 ms passed"; }, 500 ); } function tsMouseUp() { clearTimeout( timer ); }

Categories : Javascript
Angular mouse events stopped working on firefox only
Ok so i got the issue, for some unknown reason, which i'll try to research further, binding mouse event directives to elements dont work well in ng-repeat loops. It works in chrome just fine, but fails to work in firefox & IE. I suspect it has something to do with the preformance rate of the JS engine of the browser. Chromes JS engine is the fastest as of today, and it might manage the repeate

Categories : Javascript
Prevent mouse wheel events from being processed by Flash in Firefox
So I found a fix for this issue, although not entirely sure why it works. Even though Firefox supports both the "wheel" and "mousewheel" events, registering our listener to these events did not produce desired behavior. However, listening to the "DOMMouseScroll" event worked beautifully. Not sure if this is due to some browser-specific logic and/or how our legacy Flash code handles scrolling, bu

Categories : Javascript
How to combine keyboard and mouse events to create a key mapping in IntelliJ IDEA?
You need to enter a mouse shortcut, and simply use the modifier key when you click the message: "Click here to enter mouse shortcut". The mouse button plus modifier should show up under the Shortcut Preview section of the dialog.

Categories : Intellij Idea
Recently Add
How to call a method in .cs file which was implemented in code behind?
How To Prevent $variables In Restangular Post Request
Windows Phone 8 VoIP SIP Provider
How do we set the AutoCAD layer viewport override values
Load Dependent DLL using reflection
The code is not showing any error but still not displaying data on the datagrid of webform.?
Make an Image that can be flipped over in Unity 4.6
Storing JSON REST response in object
which is more efficient in conditional looping?
WebBrowser control's shortcut keys are not working
C# SQL INSERT not appearing in database table "no errors or exceptions thrown"
c# winfrom treeview class binding
How to Convert string "00h:03m:30s:793ms" to TimeSpan in c#
Calling a parameterized Stored Procedure that returns a value in C#
How to use the SQL statement to transform the data from row record to column record
How to Instantiate Enemy Prefabs Without Knowing the Name
MVC JQuery unobtrusive validation
How to draw at top left of metaFile
How to make Aggregate Root method only accessible for a Domain Event and nothing else.
how to create a list of lists from one list using LINQ?
Ignore user roles in ASP .Net MVC 5 Identity
Xpath Web scrape
add property to interface
How can I make controls added to existing mark-up be automatically hooked up to the code behind?
Too much data in C# windows form combobox, taking 30 sec to show the windows form
Creating a scrolling grid of images using WPF and XAML
PHP equivalent to asp.net routing
LINQ aggregate query
Custom Control won't fill dock correctly
Get ASP.NET Identity Current User In View
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.