Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for handled events #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bergi9
Copy link

@bergi9 bergi9 commented Jun 13, 2019

Sample code:

rawInput.KeyPressed += (sender, e) => {
    if(/*some condition*/)
    {
        e.KeyPressEvent.Handled = true;
    }
};

@stevemesser
Copy link
Owner

I tried your code with the following example:

_rawinput.KeyPressed += (sender, e) =>
{
if (e.KeyPressEvent.VKeyName == "H")
{
e.KeyPressEvent.Handled = true;
}
};

If I press H on the keyboard it still shows up in the textbox. It doesn't appear that the H key stroke is handled at all.

Am I missing something?

@stevemesser
Copy link
Owner

However, if I modify the PreMessage filter as follows:

public class PreMessageFilter : IMessageFilter
{
// true to filter the message and stop it from being dispatched
// false to allow the message to continue to the next filter or control.
public bool PreFilterMessage(ref Message m)
{
Keys latestKey = (Keys)m.WParam.ToInt64();
Console.WriteLine("Key Pressed: " + latestKey);

    if (m.Msg == Win32.WM_KEYDOWN && latestKey == Keys.H) return true;

    return false;

    //return m.Msg == Win32.WM_KEYDOWN; // This was just an example to filter all keydown presses
}

}

Then the H doesn't appear in the TextBox.

@bergi9
Copy link
Author

bergi9 commented Jun 19, 2019

I don't use the PreMessageFilter because I'm using WPF.

@stevemesser
Copy link
Owner

Oh, I don't have a WPF example anymore.

@FracturedSolace
Copy link

So does this simply keep key-presses from showing up in the Application window or is it global? I've been looking for/trying to work out a method to block keystrokes from a secondary keyboard globally.

As far as I can tell, it seems like WM_INPUT is firing after the targetted window receives the keypress, and I don't know of any way to work around this? This library https://github.com/gmamaladze/globalmousekeyhook allows for global key suppression, but THEIR method doesn't allow you to get a device ID.

Any thoughts?

@Hoopou
Copy link

Hoopou commented Nov 24, 2019

none of this works when you are not in foreground right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants