Possible for a form to detect a key press before launch

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
IFG_Infra
Posts: 9
Last visit: Fri Dec 22, 2023 12:36 pm

Possible for a form to detect a key press before launch

Post by IFG_Infra »

Product, version and build: 2019 5.6.159.0
32 or 64 bit version of product: 64
Operating system: Windows 10 1809
32 or 64 bit OS: 64

I am wondering if it is possible to deploy a powershell form executable that can detect if the CTRL button was held when launched. I am looking to have it perform a different task at startup if the CTRL button was held. So far I have tried keydown and keypress functions, but neither seem to work.

Basically my form gives users the option to select a database connection for a separate program. Once they select it and click the button, the form launches the application with the database settings, then the form plugs in a "first run" registry entry, and closes the form. The next time the form is launched it checks for that "first run" entry, and just runs the separate program without showing the database options.
I'd like to allow the user to hold CTRL while launching to re-enable the database selection. Is that possible?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Possible for a form to detect a key press before launch

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Possible for a form to detect a key press before launch

Post by davidc »

You can use the following to check if the Ctrl key is pressed:

Code: Select all

([System.Windows.Forms.Control]::ModifierKeys -band [System.Windows.Forms.Keys]::Control) -ne 0
Although, it helps to see the script itself to see what you are trying to accomplish.
David
SAPIEN Technologies, Inc.
User avatar
IFG_Infra
Posts: 9
Last visit: Fri Dec 22, 2023 12:36 pm

Re: Possible for a form to detect a key press before launch

Post by IFG_Infra »

That worked! Thank you!
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