Capture hotkeys

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Luke_F
Posts: 7
Last visit: Mon May 15, 2017 2:26 am

Capture hotkeys

Post by Luke_F »

Hi there,

for my newest project I have some minor issue where I hope I get some help from you out there.

I have a PowerShell forms script with a menue.
This menue contains a hidden menue which should be visible after hitting a hotkey like ALT+D (just an example)

so I´m looking for something like

Code: Select all

On-Hit_Event 
{
  if($_ -eq "alt+D")
  {
    $menueItem.Enable = $true
  }
}
(don´t blame me this is just a kind of pseudo-code :) )
I´ve tried to use the Keydown event from my $mainform object but had no luck. this event is never triggered.
Any ideas?

Thanks to you all in advance
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

Capture hotkeys

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, Luke_F.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Capture hotkeys

Post by dan.potter »

Luke_F wrote:Hi there,

for my newest project I have some minor issue where I hope I get some help from you out there.

I have a PowerShell forms script with a menue.
This menue contains a hidden menue which should be visible after hitting a hotkey like ALT+D (just an example)

so I´m looking for something like

Code: Select all

On-Hit_Event 
{
  if($_ -eq "alt+D")
  {
    $menueItem.Enable = $true
  }
}
(don´t blame me this is just a kind of pseudo-code :) )
I´ve tried to use the Keydown event from my $mainform object but had no luck. this event is never triggered.
Any ideas?

Thanks to you all in advance
PowerShell Code
Double-click the code block to select all.
$textbox1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
	#TODO: Place custom script here
	if ($_.alt -and $_.KeyCode -eq 'D') { Write-Host 'alt + D was pressed' }
	
}
User avatar
Luke_F
Posts: 7
Last visit: Mon May 15, 2017 2:26 am

Re: Capture hotkeys

Post by Luke_F »

Thanks for your quick response.
I may have to ask my question more precicely.
The issue I have is not about "how to identify which button is pressed" but how to define the event for it.
From your examtle the $textbox1 must be the active element to get the keys. But I need to have this working independendly on the focused item.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Capture hotkeys

Post by dan.potter »

are you using contextmenustrip or menustrip?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Capture hotkeys

Post by jvierra »

TO capture keystrokes we need to set the forms "KeyPreview" property to $true and define the keyhander on the form. This is known as a "global" keystroke handler.
PowerShell Code
Double-click the code block to select all.
$form1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
    
    if ($_.alt -and $_.KeyCode -eq 'D') { 
        Write-Host 'alt + D was pressed'
        $_.SuppressKeyPress=$true
    }
    
}
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Capture hotkeys

Post by dan.potter »

In the designer focus on the form itself and on the right set the keypreview to true.
PowerShell Code
Double-click the code block to select all.
$form1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
	#TODO: Place custom script here
	if ($_.Control -and $_.KeyCode -eq 'D') {
		Write-Host 'ctrl + D was pressed'
		
		$menuitem1ToolStripMenuItem.DropDownItems.Add('hidden1')
		
		$menuitem1ToolStripMenuItem.DropDownItems.Add('hidden2')
		
	}
}
This topic is 8 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked