• 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

    • I need someone to explain to me what "passkey" is and what happens if my device crashes and is not recoverable.
    • Editor: "YouTube is bring" should be "YouTube is bringing" in your main article title.
    • Opera files complaint against Microsoft for manipulating customers into using Edge by Usama Jawad Most of us here at Neowin are well aware of Microsoft's tactics to get its customers to use its Edge browser. It only recently stopped injecting massive banner ads for its own browser in Chrome's download portal. However, tired of Redmond's continued efforts to manipulate customers into using Edge rather than other alternatives, Opera has filed a complaint against Microsoft in Brazil. As reported by The Verge, the reason for filing a complaint against Brazil rather than any other country is that it is one of Opera's main markets, where the company enjoys a respectable third place. Opera hopes that Brazil will set a global precedent for other countries if it rules in its favor. Regarding the actual nature of the complaint, Opera alleges that Microsoft leverages "manipulative design tactics" and anti-competitive methods to force people into using Edge. Examples of this include third-party browsers not having the option of being bundled in a Windows installation, and the fact that Microsoft's own tools like Widgets, Teams, Search, and Outlook ignore a user's browser preferences and prioritize Edge when opening file like PDFs. If Brazil's regulator does rule in favor of Opera, the company will be seeking remedies like the ability to pre-install other browsers in Windows by default and restricting "dark patterns" that force customers to use Edge. This isn't Opera's first legal battle with Microsoft in recent memory. A little over a year ago, it pressured the European Union (EU) to label the Redmond tech firm's browser as a gatekeeper under the Digital Markets Act (DMA) due to the dominance of Windows in the desktop space and the inability for users to select a browser through a choice screen. However, Opera was unsuccessful in its efforts. As such, it will be interesting to see if it can score a win against Microsoft with its complaint this time.
  • Recent Achievements

    • Apprentice
      MikeK13 went up a rank
      Apprentice
    • Week One Done
      andeyhawk65 earned a badge
      Week One Done
    • First Post
      Jake2530 earned a badge
      First Post
    • Explorer
      Deranox went up a rank
      Explorer
    • Week One Done
      John Volks earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      686
    2. 2
      ATLien_0
      263
    3. 3
      Xenon
      179
    4. 4
      neufuse
      135
    5. 5
      +FloatingFatMan
      100
  • Tell a friend

    Love Neowin? Tell a friend!