dyowee

Good day. Is it possible to determine which key was pressed on the Closing event of a ToolStripDropDown since I think it can be closed using the Esc or Alt key

Thanks!



Re: Windows Forms General Which key was pressed for ToolStripDropDownCloseReason.Keyboard?

nobugz

You would have to override the ProcessDialogKey() method of the ToolStripDropDown to record the last keystroke. That is *very* awkward. Here's an alternative:

private void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e) {
if (e.CloseReason == ToolStripDropDownCloseReason.Keyboard) {
if (GetAsyncKeyState((int)Keys.Menu) < 0) Console.WriteLine("Closed by Alt");
else if (GetAsyncKeyState((int)Keys.F10) < 0) Console.WriteLine("Closed by F10");
else if (GetAsyncKeyState((int)Keys.Escape) < 0) Console.WriteLine("Closed by Esc");
else Console.WriteLine("Huh ");
}
}






Re: Windows Forms General Which key was pressed for ToolStripDropDownCloseReason.Keyboard?

dyowee

Great! Thanks a lot!



Re: Windows Forms General Which key was pressed for ToolStripDropDownCloseReason.Keyboard?

nobugz

Oops, I forgot the P/Invoke declaration for GetAsyncKeyState(). Guess you got it.





Re: Windows Forms General Which key was pressed for ToolStripDropDownCloseReason.Keyboard?

dyowee

Good day!

Is it possible to handle that the ToolStripDropDown does not close on press of the Alt button I was hoping to make it behave like the ComboBox which only closes if you press Alt+ArrowKeyUp. Thanks!