1

I am working on a C# application which consists of some context menus that have an ability to communicate with my website via WebClient(). The options work when they are clicked.

Once my app is opened, it stays open in the tray and it doesn't show in the taskbar/toolbar (the bar in the bottom where open programs stay). It's basically a background application that runs continuously.

There is a section in the context menu named Upload which includes Upload from Computer and Window screenshot. These are the two items that I want to be accessed via keyboard shortcuts. It shouldn't matter where the user is, once he clicks the set keyboard keys, he will trigger the application's _Click event for a certain context menu.

Final question: How do I make global keyboard shortcuts to trigger some context menus item _Click event?

It would be good is someone could explain a bit more broadly how to achieve this. I'm into C# for a short time (1 month learning it, 2 weeks using it) and I am having trouble understanding code just pasted here.

This is one of the click events I want to associate with a keyboard shortcut:

private void menu_upload_file_Click(object sender, EventArgs e)
{
    DialogResult dialogOpened = openFileDialog1.ShowDialog();
    if (dialogOpened == DialogResult.OK)
    {
        string filename = openFileDialog1.FileName;
        this.sendToIMGit(filename);
    }
}

Thanks.

aborted
  • 4,481
  • 14
  • 69
  • 132
  • I believe you cannot do anything like this out of the box. This would require you to attach yourself to the input device (keyboard in that very case). Actually you can only access a foreground-window or set messages to a background-window. You should check [MSDN](http://msdn.microsoft.com/en-us/library/windows/desktop/ms632590(v=vs.85).aspx) for how the message-passing of Windows works. – bash.d Feb 08 '13 at 14:21

1 Answers1

1

You will need to use a 'keyboard hook' to create a global hotkey.

See this question.

Community
  • 1
  • 1
Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
  • Thank you for answering. Unfortunately, giving me a class like that wont help me go further because of the lack of experience I have with C#, I'm still under process grasping the basics and the concept how C# works. So if you don't mind trying to explain the code, I'd really appreciate it. – aborted Feb 08 '13 at 15:13
  • Well then you certainly would be diving in at the deep end. That class wraps up some Win32 calls: these are the lower level calls that the Windows operating system offers to native (typically C++) applications. Your best bet, if you cannot work the code out, would be to find a keyboard hook class that more tidily wraps this complexity up for you. – Paul Ruane Feb 08 '13 at 15:30
  • I was thinking the same - using that class from the question you linked to and I also know that with my current knowledge, I wouldn't achieve such a complex task, even using that class is quite problematic to me. Could you show me an example use of that class? And that class also lacks something which was answered, meaning it's not complete if I just copy it over. – aborted Feb 08 '13 at 16:09
  • Do a search for 'C# keyboard hook'. There is a CodeProject example and a fair few other examples knocking around the interwebs. – Paul Ruane Feb 08 '13 at 16:27
  • I just did that and it returned me a lot of results, but I simply can't figure out how to deal with this. I thought this would help me: http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application, but I can't figure out how to connect it with the click event I provided in the question. Please bear with me. I know I'm trying to go where I shouldn't be in these beginning phases, but the app I am working on really needs this feature. – aborted Feb 08 '13 at 17:00
  • Afraid I cannot spare the time to write up a class for you to use. If you register on CodeProject you should be able to download the library and sample application for [this posting](http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook). – Paul Ruane Feb 08 '13 at 17:24
  • Well, I was not asking for writing a complete class just for me either. I just wanted to see how I could use the class linked in your answer with my `_Click` event (see question). – aborted Feb 08 '13 at 17:47
  • From http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook, how do I make a combination of keys to trigger something - CTRL + U for example? – aborted Feb 08 '13 at 18:10