enter key press

hi all,

I’ve built a GUI with a few textbox and I thought I could use the Enter key for either of those, as in;

$comboPCName_KeyPress = [System.Windows.Forms.KeyPressEventHandler]{ #Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs] if ($_.KeyChar -eq 13) { $buttonConnect.PerformClick() } }
$textboxFilterAD_KeyPress = [System.Windows.Forms.KeyPressEventHandler]{ #Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs] if ($_.KeyChar -eq 13) { $buttonGetADGroupsList.PerformClick() } }
But if the focus is on textboxFilterAD and I press Enter, the $comboPCName_KeyPress function is executed instead of $textboxFilterAD_KeyPress like I wish.
Is there something I can do to have the correct function called when I press Enter, depending on the selected control?
Thank you!

This is more of a .NET forms question, not really Powershell, but my assumption is that the Enter (and Esc) button have a separate events happening for AcceptButton. You may be able to even make it work more ‘as designed’ if you use the onFocus() event to set the AcceptButton to the required control rather than using KeyPress to change it. If you look at the second link, they are setting the AcceptButton and not parsing KeyPress events, but regardless you should be searching for .NET forms forums to find your answer if the below doesn’t work for you.

https://stackoverflow.com/questions/487920/winforms-acceptbutton-not-working
https://stackoverflow.com/questions/602876/changing-the-acceptbutton-depending-on-active-control

I was able to make it worth with what you gave me :slight_smile:

Many thanks!