• (2 Pages)
  • +
  • 1
  • 2

Global Hotkeys Register a hotkey that is triggered even when form isn't focused.

#16 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Posted 23 May 2012 - 07:39 AM

Guess I should have noted that I used 2010 as the solution. If you just needed the code, I don't think I used anything that wouldn't be available in 3.5, maybe even 2.0. You'd just have to create your own solution and copy the code over file to file.
Was This Post Helpful? 0
  • +
  • -

#17 EagleEye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 12

Posted 23 May 2012 - 08:31 AM

View PostCurtis Rutland, on 23 May 2012 - 07:39 AM, said:

Guess I should have noted that I used 2010 as the solution. If you just needed the code, I don't think I used anything that wouldn't be available in 3.5, maybe even 2.0. You'd just have to create your own solution and copy the code over file to file.


Yeah would have saved me some time though, but now your program works here and that's the most important thing :)

I'm more concerned if i'm able to implement your code into mine and get it to work. Let's see.
Was This Post Helpful? 0
  • +
  • -

#18 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Posted 23 May 2012 - 09:04 AM

It's as simple as I could make it. Check the test program for the implementation. The really important part is the WndProc method.
Was This Post Helpful? 0
  • +
  • -

#19 EagleEye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 12

Posted 23 May 2012 - 09:47 AM

View PostCurtis Rutland, on 23 May 2012 - 09:04 AM, said:

It's as simple as I could make it. Check the test program for the implementation. The really important part is the WndProc method.


Ok, thank you for the tip. I'll give it a try.
Was This Post Helpful? 0
  • +
  • -

#20 7285  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-August 12

Posted 15 August 2012 - 06:52 AM

Hello,
Nice Tutorial!

But i have a problem. I downloaded your files (worked without a problem) and tried to make it work with multiple Hotkeys, just like reaper4334 explained it. But I always get an error at:
this.hWnd = form.Handle;


which sounds like that: "Windows32Exception was not handled"
If I put that "this.hwnd = form.Handle;" in a comment i get the same error at:
Application.Run(new Form1());



The whole code:
Global Hotkey.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HotkeyWin
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}



Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hotkeys;

namespace HotkeyWin
{
    public partial class Form1 : Form
    {
        private Hotkeys.GlobalHotkey ghk;

        public Form1()
        {
            InitializeComponent();
            ghk = new Hotkeys.GlobalHotkey(Constants.NOMOD + Constants.NOMOD, Keys.F1, this);
        }

        //private void HandleHotkey()
        //{
        //    WriteLine("Hotkey pressed!");
        //}

        //protected override void WndProc(ref Message m)
        //{
        //    if (m.Msg == Hotkeys.Constants.WM_HOTKEY_MSG_ID)
        //        HandleHotkey();
        //    base.WndProc(ref m);
        //}

        private Keys GetKey(IntPtr LParam)
        {
            return (Keys)((LParam.ToInt32()) >> 16);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
            {
                switch (GetKey(m.LParam))
                {
                    case Keys.F1:
                        WriteLine("Hotkey pressed!");
                        break;
                }
                base.WndProc(ref m);
            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            WriteLine("Trying to register SHIFT+ALT+O");
            if (ghk.Register())
                WriteLine("Hotkey registered.");
            else
                WriteLine("Hotkey failed to register");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!ghk.Unregiser())
                MessageBox.Show("Hotkey failed to unregister!");
        }

        private void WriteLine(string text)
        {
            textBox1.Text += text + Environment.NewLine;
        }
    }
}



Im using Windows 7 64bit, .NET Framework 4.0 and Microsoft Visual C# 2010 Express.

What am i doing wrong?

This post has been edited by modi123_1: 15 August 2012 - 06:53 AM
Reason for edit:: fixed botched code tags

Was This Post Helpful? 0
  • +
  • -

#21 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Posted 15 August 2012 - 08:11 AM

Well, first I suggest going to my linked GitHub page and using the latest version of the library.

You can also see a more current example there:

https://github.com/c...s.Test/Form1.cs
Was This Post Helpful? 0
  • +
  • -

#22 mma1979  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-July 11

Posted 25 August 2012 - 08:10 PM

It just very good article but I have that case
I need to fire some events with key enter
but when I do that the original function of key enter
is overridden and not works in other applications like in ms-word
or chatting.
is there way to do both
Was This Post Helpful? 0
  • +
  • -

#23 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Posted 26 August 2012 - 11:30 AM

I don't know, and I can't imagine a non-malicious use for hotkeying Enter.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2