473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Capturing a TAB key press event

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
Nov 17 '05 #1
9 27053
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" <pr*********@hotmail-dot-com.no-spam.invalid> a écrit dans le
message de news:42**********@127.0.0.1...
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

Nov 17 '05 #2
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
Nov 17 '05 #3
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" <pr*********@hotmail-dot-com.no-spam.invalid> a écrit dans le
message de news:42**********@127.0.0.1...
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

Nov 17 '05 #4
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
Nov 17 '05 #5
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
Nov 17 '05 #6
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
Nov 17 '05 #7
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
Nov 17 '05 #8
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
Nov 17 '05 #9
Desperate Bump

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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mukesh | last post by:
Hi I have a datagrid in which i want to capture the key press event and find which row i am in. Tried with the key press event provided by datagrid, but it does not fire. What could be wrong ...
0
by: Joecx | last post by:
I am capturing the click event using Overrides OnClick. This captures the click event in the open or unused areas of the form, but it doesn't capture the click event in the textboxes etc. Do I...
1
by: donald | last post by:
I got a ASP.net text box called textbox1 and a label called label1. When a key is press in a text box i want it to up date the label with what is in the textbox. Label1.Text = TextBox1.Text; ...
6
by: salo | last post by:
Can anybody say me for writing javascript code for textbox key press event such a way that it should allow only alphabets,dot and spaces
10
by: ofiras | last post by:
Hii, I saw a lot of programs, that can be in tray (in the bottom right corner), and they can get Key Press event although they are not in focus. Is there a way to do it in C# windows application?...
3
by: fieldling | last post by:
I've got a command button on a form that I would also like to work if I press, for example, F5. (Sounds pointless having a command button I know but there you go). I've tried using the On Key...
2
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
I have a tabcontrol where I want to capture the CTRL+C key combination. My tabcontrol has on its tabpages some treeView controls. My intention is to intercept the event of pressing the CTRL+C when...
1
by: Sendil kumar | last post by:
When I do a Ctrl-C(copy) on a text, the text was not copied. By narrowing down the problem I found that VC++ message map is not capturing the Ctrl-C key press event(Only captures the first key press,...
7
by: thomask | last post by:
Hello my friends , How to implement key press event for checking whether the value entered in the text box is integer.(ie if we press any alphabetic letter in the text box, at the key press it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.