Connecting Tech Pros Worldwide Forums | Help | Site Map

Capturing a TAB key press event

supster
Guest
 
Posts: n/a
#1: Nov 17 '05
Hello, I'm trying to capture when users press the tab key. This is
scrolling through different fields with tab is broken in my form. I'm
not sure why, but it may have something to do with the fact that the
Windows Shell is creating the form (it is defined in a DLL). I copy
and pasted the form's code over to another application which created
the form through an executable, and tabbing worked fine - so that is
my only guess.

Anyway, I'd like to be able to manually recreate the tabbing feature
(including shift+tab and ctrl+tab), I have no problem writing the
functions to do this, however I cannot find out when tab (or
shift+tab) is pressed.

The KeyPressed event is not called, and the only solution I have found
is the following:

[System.Runtime.InteropServices.DllImport("User32",
EntryPoint="GetKeyState", ExactSpelling=false,
CharSet=System.Runtime.InteropServices.CharSet.Ans i,
SetLastError=true)]
private static extern int GetKeyState(long nVirtKey);
private const int VK_TAB = 0X9;
private const int VK_SHIFT = 0X10;

...

while(true)
{
iRetVal = GetKeyState(VK_TAB);
if ((iRetVal == -128 || iRetVal == -127) &&
this.isFocused)
{
MessageBox.Show("Tab Key Pressed");
}
System.Threading.Thread.Sleep(25);
}


I would rather not have a thread running this the whole time the form
is open. Also, another problem with this method is that it is called
even when the form is not the focused window, and the &&
this.isFocused does not seem to help this at all.

If anyone has a better solution, please let me know!

Thanks


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Ludovic SOEUR
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Capturing a TAB key press event


for each control that do not catch TAB key, override IsInputKey method like
following :
protected override bool IsInputKey(Keys keyData) {
if (keyData==Keys.Tab) return true;
return base.IsInputKey (keyData);
}

Now, KeyDown event will be called if TAB key is pressed.

Hope it helps,

Ludovic Soeur.



"supster" <prelugejunk@hotmail-dot-com.no-spam.invalid> a écrit dans le
message de news:42428a06$1_1@127.0.0.1...[color=blue]
> Hello, I'm trying to capture when users press the tab key. This is
> scrolling through different fields with tab is broken in my form. I'm
> not sure why, but it may have something to do with the fact that the
> Windows Shell is creating the form (it is defined in a DLL). I copy
> and pasted the form's code over to another application which created
> the form through an executable, and tabbing worked fine - so that is
> my only guess.
>
> Anyway, I'd like to be able to manually recreate the tabbing feature
> (including shift+tab and ctrl+tab), I have no problem writing the
> functions to do this, however I cannot find out when tab (or
> shift+tab) is pressed.
>
> The KeyPressed event is not called, and the only solution I have found
> is the following:
>
> [System.Runtime.InteropServices.DllImport("User32",
> EntryPoint="GetKeyState", ExactSpelling=false,
> CharSet=System.Runtime.InteropServices.CharSet.Ans i,
> SetLastError=true)]
> private static extern int GetKeyState(long nVirtKey);
> private const int VK_TAB = 0X9;
> private const int VK_SHIFT = 0X10;
>
> ..
>
> while(true)
> {
> iRetVal = GetKeyState(VK_TAB);
> if ((iRetVal == -128 || iRetVal == -127) &&
> this.isFocused)
> {
> MessageBox.Show("Tab Key Pressed");
> }
> System.Threading.Thread.Sleep(25);
> }
>
>
> I would rather not have a thread running this the whole time the form
> is open. Also, another problem with this method is that it is called
> even when the form is not the focused window, and the &&
> this.isFocused does not seem to help this at all.
>
> If anyone has a better solution, please let me know!
>
> Thanks
>
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com[/color]


supster
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Capturing a TAB key press event


Awsome, this works... however is there any easier way to catch tab
pressed anywhere on the form other than overriding the method for
each possible control that may be focused?
Posted at: http://www.groupsrv.com

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Ludovic SOEUR
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Capturing a TAB key press event


There is a simple way, maybe it is what you were looking for. The trick is
the use of Application AddMessageFilter method. You only have to design your
own TabKeyCatcher class to do exactly what you want.
Use the following lines :

using System;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form {
...
...
...
[STAThread]
static void Main() {
Application.AddMessageFilter(new TabKeyCatcher());
Application.Run(new Form1());
}
...
...
...
public Form2() {
InitializeComponent();

...
...
}
private void InitializeComponent() {
...
...
}
...
...
...
}

class TabKeyCatcher:IMessageFilter {
public const int WM_KEYDOWN=0x0100;
public bool PreFilterMessage(ref Message m) {
if (m.Msg==WM_KEYDOWN) {
if (m.WParam.ToInt32()==(int)Keys.Tab) {
MessageBox.Show("Tab Key was pressed");
}
}
return false;
}
}

Hope it helps,

Ludovic Soeur.


"supster" <prelugejunk@hotmail-dot-com.no-spam.invalid> a écrit dans le
message de news:4243db7d$1_5@127.0.0.1...[color=blue]
> Awsome, this works... however is there any easier way to catch tab
> pressed anywhere on the form other than overriding the method for
> each possible control that may be focused?
> Posted at: http://www.groupsrv.com
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com[/color]


supster
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Capturing a TAB key press event


Thanks a lot!

However, I don't have a main function. The form is created by a call
from another program (this is a DLL). Can I call
Application.AddMessageFilter from the form's constructor?
Posted at: http://www.groupsrv.com

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
supster
Guest
 
Posts: n/a
#6: Nov 17 '05

re: Capturing a TAB key press event


Ok, well now I need to check if shift and control are down or not :)

Is there any better way to check other than something like:


if (m.Msg==WM_KEYDOWN)
{
if (m.WParam.ToInt32()==(int)Keys.Shift)
shiftDown = true;
}

if (m.Msg==WM_KEYUP)
{
if (m.WParam.ToInt32()==(int)Keys.Shift)
shiftDown = false;
}

Posted at: http://www.groupsrv.com

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
supster
Guest
 
Posts: n/a
#7: Nov 17 '05

re: Capturing a TAB key press event


Ok, well now I need to check if shift and control are down or not :)

Is there any better way to check other than something like:


if (m.Msg==WM_KEYDOWN)
{
if (m.WParam.ToInt32()==(int)Keys.Shift)
shiftDown = true;
}

if (m.Msg==WM_KEYUP)
{
if (m.WParam.ToInt32()==(int)Keys.Shift)
shiftDown = false;
}

Posted at: http://www.groupsrv.com

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
supster
Guest
 
Posts: n/a
#8: Nov 17 '05

re: Capturing a TAB key press event


Well surprise surprise... When I tested this earlier, I tested it in
my debugger program which is a simple windows application with a form
that simulates the Windows Shell making a call onto my DLL.

Everything worked fine and dandy.

Just now, I implemented this into my DLL to test it, and no go.
PreFilterMessage() is never even called, I think the Windows shell
reroutes messages to it's own function and it never gets to my
PreFilterMessage().

Anyone have any other ideas?
Posted at: http://www.groupsrv.com

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
supster
Guest
 
Posts: n/a
#9: Nov 17 '05

re: Capturing a TAB key press event


Well surprise surprise... When I tested this earlier, I tested it in
my debugger program which is a simple windows application with a form
that simulates the Windows Shell making a call onto my DLL.

Everything worked fine and dandy.

Just now, I implemented this into my DLL to test it, and no go.
PreFilterMessage() is never even called, I think the Windows shell
reroutes messages to it's own function and it never gets to my
PreFilterMessage().

Anyone have any other ideas?
Posted at: http://www.groupsrv.com

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
supster
Guest
 
Posts: n/a
#10: Nov 17 '05

re: Capturing a TAB key press event


Desperate Bump

:(
Posted at: http://www.groupsrv.com

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Closed Thread