CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2015
    Posts
    5

    Hotkeys and Hotstrings

    Ok so let me first say that I am new to programming and even newer to C#. I have been using a language called AHK to create applications and scripts but I want to expand into C#. For now, I'd really like to know how to create hotkeys and hotstrings in C#. Whether it be running a particular program when F12 is pressed or sending a string when typing a few characters. Please forgive my ignorance with this language.

  2. #2
    Join Date
    Feb 2015
    Posts
    5

    Re: Hotkeys and Hotstrings

    Ok, guess this is too simple of a question for this forum.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Hotkeys and Hotstrings

    Quote Originally Posted by bp247ind View Post
    Ok, guess this is too simple of a question for this forum.
    Have you tried searching bing or google for "hotkeys in C#" ?

  4. #4
    Join Date
    Feb 2015
    Posts
    5

    Re: Hotkeys and Hotstrings

    Quote Originally Posted by Arjay View Post
    Have you tried searching bing or google for "hotkeys in C#" ?
    Of course I have. I gave two examples of what I want to do with these hotkeys because when searching for "hotkey in c#"; none of the examples are accomplishing what I am trying to do.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Hotkeys and Hotstrings

    It's hard to help if we don't know what you've tried in terms of the code you are using. With regard to F12, keep in mind that when running a C# app under visual studio, F12 will cause the program to break into debug mode.

    So, I'd recommend trying to get other hotkeys working before attempting F12.

  6. #6
    Join Date
    Feb 2015
    Posts
    5

    Re: Hotkeys and Hotstrings

    Quote Originally Posted by Arjay View Post
    It's hard to help if we don't know what you've tried in terms of the code you are using. With regard to F12, keep in mind that when running a C# app under visual studio, F12 will cause the program to break into debug mode.

    So, I'd recommend trying to get other hotkeys working before attempting F12.
    The F12 was just an example to illustrate the idea. I havent written any code because, like I said, I am literally just starting into this language. Any tutorials that I have been working thru deal with the Console class. I understand that it will take some time and effort to learn a new language and I am fine with that but for now I'd really like to learn this simple example to use and tweak as I am also learning through tutorials.

    Just to be able to help with some work automation. Example: I have to write out the same 100 character "phrase" at work many times per day and I'd like to have my script write that out (within any other application such as notepad or web browser) by just pressing a single key or perhaps a hotstring like typing "myphrase" and the app returns the full 100 characters.

    Again, this is just a simple example just so you know what I am trying to accomplish

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Hotkeys and Hotstrings

    For global hotkeys in C#, search bing or google for "Gma.UserActivityMonitor" for a code project example. Also read the stackoverflow entry for how to fix an issue with the project when used on .Net 4.5.

    For automating apps, search msdn for UIAutomation and/or Active Accessibility.

  8. #8
    Join Date
    Apr 1999
    Location
    Davenport, Iowa, USA
    Posts
    158

    Re: Hotkeys and Hotstrings

    Maybe you could do something like this in the KeyDown event?
    PHP Code:
            private void Form1_KeyDown(object senderKeyEventArgs e)
            {
                if (
    e.KeyCode == Keys.F12)
                {
                    if (
    e.Control == true)
                    {
                        
    MessageBox.Show("Control + F12 pressed!");
                    }
                    else if (
    e.Alt == true)
                    {
                        
    MessageBox.Show("Alt + F12 pressed!");
                    }
                    else if (
    e.Shift == true)
                    {
                        
    MessageBox.Show("Shift + F12 pressed!");
                    }
                    else
                    {
                        
    MessageBox.Show("F12 pressed");
                    }
                }
            } 
    Hope this helps!
    Matt

  9. #9
    Join Date
    Feb 2015
    Posts
    5

    Re: Hotkeys and Hotstrings

    Quote Originally Posted by Matt Hauser View Post
    Maybe you could do something like this in the KeyDown event?
    PHP Code:
            private void Form1_KeyDown(object senderKeyEventArgs e)
            {
                if (
    e.KeyCode == Keys.F12)
                {
                    if (
    e.Control == true)
                    {
                        
    MessageBox.Show("Control + F12 pressed!");
                    }
                    else if (
    e.Alt == true)
                    {
                        
    MessageBox.Show("Alt + F12 pressed!");
                    }
                    else if (
    e.Shift == true)
                    {
                        
    MessageBox.Show("Shift + F12 pressed!");
                    }
                    else
                    {
                        
    MessageBox.Show("F12 pressed");
                    }
                }
            } 
    Hope this helps!
    Matt
    Thank you Matt! I tend to learn better when I can visually see the code to be able to know what it's actually doing. Much Appreciated!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured