• 0

[C#] How do I create some hotkeys inside my app?


Question

I do not want to create global hotkeys that work in the whole system. I have this note taking app which uses a TextBox to display the note (from a text file) and for now, I want only 2 internal shortcuts.

Ctrl + A: To select the whole text in the TextBox

Ctrl + S: To automatically save the note to the text file

I only want both hotkeys to work if the TextBox control has focus...

Any help is appreciated.

12 answers to this question

Recommended Posts

  • 0
  Betaz said:

That looks interesting, I'll take a better look at that some other time cause it's running late, very late...

I'll leave a reply here, if I, for some reason, have a problem with the above article.

  • 0

I took a look at the example, at it seems as though it should work fine. If you don't need to have many keyboard shortcuts though, you could likely shorten it to something simpler. The following is an example:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
	switch (keyData)
	{
		case Keys.Control|Keys.S:
			MessageBox.Show("control
			return true;
		case Keys.Control|Keys.A:
			MessageBox.Show("control
			return true;
	}
	return false;
}

  • 0
  cowwithoutbrain said:
I took a look at the example, at it seems as though it should work fine. If you don't need to have many keyboard shortcuts though, you could likely shorten it to something simpler. The following is an example:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
 {
	 switch (keyData)
	 {
		 case Keys.Control|Keys.S:
			 MessageBox.Show("control
			 return true;
		 case Keys.Control|Keys.A:
			 MessageBox.Show("control
			 return true;
	 }
	 return false;
 }

That's very true, and that may be a better solution for the case. I didn't go particularly in depth in choosing the article, just found one that explained accelerators well enough. You can always process directing in the ProcessCmdKey override just as easily.

  • 0
  Nazgulled said:
So, what's the point on having all that extra code? Is it just to organize the code and the keys better? For now, I have these 2 shortcuts, in the future, I can have more, I don't know...

The example is given to explain all the different parts. It's broken up into each piece for organization, but even more so to break it into pieces to teach with the article. As with just about anything in coding, you can expand anything ridiculously, or you can compress it to almost nothing. It just depends on how you feel comfortable with your code.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • RoboForm 9.7.7 by Razvan Serea RoboForm is the top-rated Password Manager and Web Form Filler that completely automates password entering and form filling. RoboForm makes logging into Web sites and filling forms faster, easier, and more secure. RoboForm memorizes and securely stores each user name and password the first time you log into a site, then automatically supplies them when you return. RoboForm's powerful Logins feature eliminates the manual steps of logging into any online account. With just one click RoboForm will navigate to a Web site, enter your username and password and click the submit button for you. Completing long registration or checkout forms is also a breeze. Simply click on your RoboForm Identity and RoboForm fills-in the entire form for you. You no longer need to remember all your passwords. You remember one Master Password, and RoboForm remembers the rest. This allows you to use stronger passwords, making your online experience more secure. RoboForm uses strong AES encryption for complete data security. The all new RoboForm comes with Chrome and Safari browser support, iPhone/iPad and Android support, as well a brand new RoboForm Everywhere license for use on unlimited computers and mobile devices. RoboForm 9.7.7 changelog: Show RF Desktop unlock UI when user selects "Unlock" in RF Desktop UI. Fixed blank icon appeared in the Windows taskbar for RF Editor. Miscellaneous bug fixes. Download: RoboForm 9.7.7 | 42.2 MB (Free, paid upgrade available) View: RoboForm Website Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • LG G5 was the last memorable phone I had. The Samsung and Pixels and have had since have been disappointing and boring.
    • Isn't the CPU used to calculate the parity for the RAID? If so, the combination of SSDs and 10GBe might make the CPU more important
    • yeah GSMA began working to enable end to end encryption between android and iphone last year and apparently a new standard was developed. apple has said that they would implement this in "future software updates" but i haven't heard anything since march, the time this was all reported on. shortly after, i read on forbes that the FBI suggests not sending texts between iphone and android because they're unencrypted. i use signal to chat with my wife but i'd rather just use messages tbh (she has an iphone), i'm not really a 3rd party guy haha
    • Well, I did not like the trailer for the project he went to work on also, but why do you think he should waste time with this… did you love the season 2? Maybe I am missing out after the crap I saw in first season ep1-3? I love the first last of us game… while not the BEST it was one of the games that I will remember for the EXPERIENCE it game me… last of us 2 was not on the same level at all and the show🤔 complete miss in my experience of the first few level
  • Recent Achievements

    • Reacting Well
      SteveJaye earned a badge
      Reacting Well
    • One Month Later
      MadMung0 earned a badge
      One Month Later
    • One Month Later
      Uranus_enjoyer earned a badge
      One Month Later
    • Week One Done
      Philsl earned a badge
      Week One Done
    • Week One Done
      Jaclidio hoy earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      431
    2. 2
      ATLien_0
      156
    3. 3
      +FloatingFatMan
      149
    4. 4
      Nick H.
      64
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!