• 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

    • On the 25H2 release, the "Insider Preview" references in the system has been removed. This usually indicates that the public release will be ready within 2 months.
    • Microsoft: Windows Autopatch is the safest way to upgrade enterprise PCs to Windows 11 by Usama Jawad A few hours ago, Microsoft published a guide for IT admins explaining how they can use Intune to upgrade Windows 10 devices to Windows 11, while also migrating from Active Directory (AD) to a cloud-native system like Entra ID. The company has also published a similar guide, but switched the tool to Windows Autopatch, claiming that it is the fastest and safest way for enterprise PCs to update to Windows 11. For those unaware, Windows Autopatch is a way to automate updates while empowering IT admins to ensure that endpoints are healthy and compliant through ring-based, staggered deployments. IT admins also have the ability to reverse updates easily if something does go wrong. In the current scenario of upgrading enterprise PCs to Windows 11 using Autopatch, Microsoft has outlined a four-step process. The first involves assessing Windows 11-readiness across your organization, assigning Entra ID groups to devices, and then mapping these groups to rollout rings in Autopatch. Next, IT admins should segment devices into Windows Autopatch groups, while also defining staggered rollout policies controlled through rollout rings. At a base level, there should be two groups: devices that meet the criteria of Windows 11 and should upgrade to it, and Windows 10 hardware that doesn't meet the criteria and should receive Extended Security Updates (ESUs). Devices should be spread in a logical manner across various rings, with each group having a dedicated update policy. The third step involves defining the speed of staggered update rollouts. This can be managed through the Intune admin center, which gives you control over sequencing, pace, and deferrals. Finally, IT admins should monitor the rollout of the Windows 11 update through the Windows Autopatch feature update reporting module. It contains the update status across devices, trendlines within historical views, and remediation guidance for errors. Microsoft believes that this combination of Windows Autopatch groups and Intune is the best way to upgrade to Windows 11, so IT admins should get started right away as support for Windows 10 is ending on October 14, 2025.
    • TDP of this CPU is 60 watts higher than Ryzen 7600. At s usage rate of four hours per day, at a cost of twelve cents per KWh, the Intel cost $10.51 more per year to use. I don't see a real advantage to Intel here.
    • Lmao. Cries about not playing those games not installed and yet don't ever want to touch them.
    • If I want to merge folder trees that have a similar structure, Beyond Compare is always my first choice. It's not free but it's awesome. If I want to just scan a whole drive/folder and find duplicates that are taking up space, I like Czkawka.
  • Recent Achievements

    • First Post
      Gladiattore earned a badge
      First Post
    • Reacting Well
      Gladiattore earned a badge
      Reacting Well
    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      650
    2. 2
      ATLien_0
      261
    3. 3
      Xenon
      165
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      107
  • Tell a friend

    Love Neowin? Tell a friend!