• 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.

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 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 s");
			return true;
		case Keys.Control|Keys.A:
			MessageBox.Show("control a");
			return true;
	}
	return false;
}

Link to comment
Share on other sites

  • 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 s");
			 return true;
		 case Keys.Control|Keys.A:
			 MessageBox.Show("control a");
			 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.

Link to comment
Share on other sites

  • 0

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...

Link to comment
Share on other sites

  • 0
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.

Link to comment
Share on other sites

  • 0
Hello I am in need of hotkeys as well.

I have a question, how do I make it the hotkeys work if I am not focused on the window?

You implement a global keyboard hook. Writing a keylogger are we? :)

Link to comment
Share on other sites

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

    • No registered users viewing this page.