multiple keystrokes to open child form

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 5 years and 1 month 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
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

multiple keystrokes to open child form

Post by apowershelluser »

Hello,

I'm not sure how to word this to find a result on google, but essentially I'd like to enter in '↑↑↓↓←→←→BA' to open a child form

Any chance of how this can be done?

To be clear, I want to press:
up arrow
up arrow
down arrow
down arrow
left arrow
right arrow
left arrow
right arrow
B
A
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: multiple keystrokes to open child form

Post by davidc »

Yes, you can accomplish this by using an array that keeps track of the sequence of keys. In the form's KeyDown event, check if the KeyCode matches:

Code: Select all

$script:KeyPressSequence = @('Up', 'Down', 'Up', 'Down', 'Left', 'Right', 'Left', 'Right', 'A', 'B', 'A', 'B')
$script:KeySequenceIndex = 0

$form1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
	#TODO: Place custom script here
	Write-Host $_.KeyCode
	
	if ($_.KeyCode -eq $script:KeyPressSequence[$script:KeySequenceIndex])
	{
		#increment the sequence
		$script:KeySequenceIndex++
		
		#Did we finish the sequence?
		if ($script:KeySequenceIndex -ge $script:KeyPressSequence.Length)
		{
			$script:KeySequenceIndex = 0; #Reset counter
			#Show the Form here
			Write-Host 'Show the child Form'
		}
	}
}
You will also have to set the form's KeyPreview property to True.

Code: Select all

$form1.KeyPreview = $True
David
SAPIEN Technologies, Inc.
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: multiple keystrokes to open child form

Post by apowershelluser »

perfect, thank you

confirmed it works
This topic is 5 years and 1 month 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