Author Topic: Detect key press while acting on a selected item in a form  (Read 2013 times)

0 Members and 1 Guest are viewing this topic.

AKS

  • Guest
Detect key press while acting on a selected item in a form
« on: February 26, 2012, 08:14:01 PM »
I have a modeless form that lists all the open dwgs in a listbox. It is supposed to be a one stop shop for various dwg activities like switching, opening, saving, closing and so on. The application needs to detect what keyboard key happens to be pressed while a form's button is also pressed so that the button's event can perform more than one task if the user knows how to use it that way. I had poor results fooling with setting a flag using the listbox's keyboard events. What I need is a simple keyboard state class that can report the last keypress and maybe even clear it if so desired.   

fixo

  • Guest
Re: Detect key press while acting on a selected item in a form
« Reply #1 on: February 26, 2012, 11:57:32 PM »

On Form_Load try to add some lines one by one or use foreach to iterate
through all controls
Code: [Select]
textBox1.KeyPress += new KeyPressEventHandler(catch_KeyPress);
 textBox2.KeyPress += new KeyPressEventHandler(catch_KeyPress);
 textBox3.KeyPress += new KeyPressEventHandler(catch_KeyPress);
 comboBox1.KeyPress += new KeyPressEventHandler(catch_KeyPress);
 comboBox2.KeyPress += new KeyPressEventHandler(catch_KeyPress);
 comboBox3.KeyPress += new KeyPressEventHandler(catch_KeyPress);
ETC...

then add this common event handler for all
Code: [Select]
private void catch_KeyPress(object sender, KeyPressEventArgs e)
{
         Control ctl = (Control)sender;
         MessageBox.Show("Key pressed on: " + ctl.ToString());
         // rest you code....
}
   

~'J'~

AKS

  • Guest
Re: Detect key press while acting on a selected item in a form
« Reply #2 on: February 27, 2012, 01:48:38 PM »
Ah ha, I see. I'll give it a shot. Do I need to remove the handler on the way out?

Thank you.

AKS

  • Guest
Re: Detect key press while acting on a selected item in a form
« Reply #3 on: February 27, 2012, 02:13:13 PM »
I hate to be asking this out of ignorance.  It works but apparently the .KeyPress event does not occur for keys like Shift, Ctrl etc. The keys I want to detect need to be easy to get to with the least amount of physical and mental effort. The single keys at the keyboard edge like Shift and Ctrl are best. Is there an event for these?

AKS

  • Guest
Re: Detect key press while acting on a selected item in a form
« Reply #4 on: February 27, 2012, 02:18:24 PM »
Ok, I got it. .Keydown

Thanks again

fixo

  • Guest
Re: Detect key press while acting on a selected item in a form
« Reply #5 on: February 27, 2012, 04:28:56 PM »
Ok, I got it. .Keydown

Thanks again
Sorry I was very busy  :oops: