WinForms keyboard onpress capture.

J

JDeats

I'm trying to figure out how best to handle a combination of key
events for WinFoms (C# 2.0 VS.NET 2005). I have
created a MessageFilter to parse out parameters from the API
WM_KEYDOWN message and I am applying in Program.cs Main() with:
Application.AddMessageFilter(... See below for partial source code for
my message filter.

My problem is with this method there are a few keys I can trap on.
Specifically the 'Alt' key and the 'Fn' key found on my notebook
computers is a real problem. I need my WinForms application to trap on
the keyboard sequence a user would go through to trigger the Windows
OS screen capture function (Fn + PrtSc on notebook or just Alt PrtScr
on Desktop). What would be the best method to do this?



public class KeyboardMsgFilter : IMessageFilter
{
public delegate void KeyboardEvent(object source, string
keyVal);
public event KeyboardEvent KeyPressed;


public bool PreFilterMessage(ref Message m)
{
string key = "";

const int WM_KEYDOWN = 0x0100;
// Blocks all the messages relating to the left mouse
button.

if (m.Msg == WM_KEYDOWN)
{

int keycode = Convert.ToInt32(m.LParam.ToString());
switch (keycode)
{
case 1179649 :
key = "E";
break;
case 1245185 :
key = "R";
break;
 
J

Jeff Johnson

I'm trying to figure out how best to handle a combination of key
events for WinFoms (C# 2.0 VS.NET 2005). I have
created a MessageFilter to parse out parameters from the API
WM_KEYDOWN message and I am applying in Program.cs Main() with:
Application.AddMessageFilter(... See below for partial source code for
my message filter.

My problem is with this method there are a few keys I can trap on.

I assume you meant "canNOT trap on."
Specifically the 'Alt' key and the 'Fn' key found on my notebook
computers is a real problem.

It has been my (limited) experience that Alt+key shortcuts are simply NOT
trappable using Windows Forms. In order to trap a keypress including the Alt
key, at least one other modifier key (Ctrl/Shift) must be present. .NET
seems to be doing some pre-pre-pre-filtering and just doesn't let you get a
bare Alt+something. Hopefully someone has a solution, as I'd be interested
myself.
 
J

JDeats

I assume you meant "canNOT trap on."


It has been my (limited) experience that Alt+key shortcuts are simply NOT
trappable using Windows Forms. In order to trap a keypress including the Alt
key, at least one other modifier key (Ctrl/Shift) must be present. .NET
seems to be doing some pre-pre-pre-filtering and just doesn't let you geta
bare Alt+something. Hopefully someone has a solution, as I'd be interested
myself.

Thanks.... anyone?
 
M

mm

JDeats said:
I'm trying to figure out how best to handle a combination of key
events for WinFoms (C# 2.0 VS.NET 2005). I have
created a MessageFilter to parse out parameters from the API
WM_KEYDOWN message and I am applying in Program.cs Main() with:
Application.AddMessageFilter(... See below for partial source code for
my message filter.

My problem is with this method there are a few keys I can trap on.
Specifically the 'Alt' key and the 'Fn' key found on my notebook
computers is a real problem. I need my WinForms application to trap on
the keyboard sequence a user would go through to trigger the Windows
OS screen capture function (Fn + PrtSc on notebook or just Alt PrtScr
on Desktop). What would be the best method to do this?

Have a look at:

Form methods KeyPreview ad KeyDown and the Keys enumeration - this will
do everything you need. Set up a listener like you are doing, something like

// activate key listener
<your form>.KeyPreview = true; // VIP!!! Form previews key
presses
<your form>.KeyDown += new
KeyEventHandler(ProcessKeyPress); //

and then in your

private void ProcessKeyPress(object sender, KeyEventArgs kea)
{
kea.Handled = true; // disable othe controls from
receiving keystrokes
// test for (Alt key) and (Ctrl key) and whatever else you want
if (kea.Alt) ...
else if (kea.Control) ...
etc.

The Keys enum has entries for both Fn<xx> and PrtScrn.

HTH, Matt.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

headache with listview 2
track page in listview 6

Top