• 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

    • Microsoft accidentally replaced Windows 11 startup sound with one from Vista by Taras Buria The recently released Windows 11 Dev and Beta builds introduced some welcome changes and improvements. However, those preview builds are not flawless and have a pretty long list of known issues. One of those issues, though, is a rather delightful one: Windows 11's default startup jingle has been accidentally replaced with one from 2006. After Windows Insiders discovered that Windows 11 now plays the Windows Vista startup sound and reported it to Microsoft, the company acknowledged it and added it to the list of known bugs in the latest Windows 11 Dev and Beta builds: This week’s flight comes with a delightful blast from the past and will play the Windows Vista boot sound instead of the Windows 11 boot sound. We’re working on a fix. Although Windows Vista is nearly two decades old, it was brought to everyone's attention this week after Apple introduced macOS 26 Tahoe with its controversial "Liquid Glass" redesign, which many consider a rather miserable remix of Windows Aero from Windows Vista and Windows 7. While it is definitely an interesting coincidence, Microsoft did not intentionally replace the startup sound in Windows 11 preview builds. Brandon LeBlanc from the Windows Insider team confirmed in his X that that is a bug after joking about everyone talking about Windows Vista once again in light of Apple's latest announcements: It is worth noting that if you miss the startup sound of Windows Vista, you can still use it in modern Windows versions. All it takes is the original WAV file and a few clicks in the Windows Registry and Sound settings. And for those who want a shot of nostalgia, here is the sound of Windows Vista startup: What startup jingle do you like more: Windows Vista or Windows 11? Share your thoughts in the comments.
    • I refused at $70, $60 was cutting it close for me and I only bought one game at that price, CP2077.
    • Markets are always a follow the leader type of place so to speak. MS didn't raise prices first, but after everyone else did, they will to. Besides, at least they're doing other pro-consumer moves like Play Anywhere. In this case, you can think of it as $80 for 2 copies (console and PC) while Sony and Nintendo would just charge you for both, so $160. Regardless who does it, $80 is too much, even $70 is IMO. Lots of games still come out at $50 and sell well, even if your budget is higher, at $50 the odds are you'll sell more copies and just make it up on volume.
    • How about letting us do an in place upgrade from recovery that would be amazing.
    • From an actual owner (ME) this does not work even remotely good at low speeds.
  • Recent Achievements

    • Apprentice
      Wireless wookie went up a rank
      Apprentice
    • Week One Done
      bukro earned a badge
      Week One Done
    • One Year In
      Wulle earned a badge
      One Year In
    • One Month Later
      Wulle earned a badge
      One Month Later
    • One Month Later
      Simmo3D earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      558
    2. 2
      ATLien_0
      257
    3. 3
      +FloatingFatMan
      180
    4. 4
      Michael Scrip
      124
    5. 5
      Steven P.
      97
  • Tell a friend

    Love Neowin? Tell a friend!