Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Detecting music keys from keyboard in winform app

Detecting music keys from keyboard in winform app

Scheduled Pinned Locked Moved C#
csharplinqgraphicsdockerjson
8 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wheels012
    wrote on last edited by
    #1

    Good morning. I am trying to detect the music button events from the keyboard, but have been unsuccessful so far. I have the following code:

    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 System.Runtime.InteropServices;
    using System.IO;
    using System.Collections;

    namespace KPlayer
    {
    public partial class Form1 : Form
    {
    WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
    WMPLib.IWMPPlaylist pl;
    //WMPLib.IWMPPlaylistArray plItems;
    WMPLib.IWMPMedia media;

        // Required designer variable
        //private System.ComponentModel.Container components = null;
    
        ArrayList ar = new ArrayList();
        int intCount = 0;      
        List<int> numbers = new List<int>();
      
        float currentSize = 10;
        string currentFont = "DS-Digital";
    
        public Form1()
        {
            // Required for Windows Form Designer support
            InitializeComponent();
        }
       
        \[STAThread\]
        // Declare Windows API calls used to access Windows hooks
        \[DllImport("user32.dll")\]
    
        public static extern int SetWindowsHookEx(int hookType,
                                                   HookProc callback,
                                                   int instance,
                                                   int threadID);
        \[DllImport("user32.dll")\]
        public static extern int CallNextHookEx(int hookHandle, int code,
                                                int wparam, int lparam);
        \[DllImport("user32.dll")\]
        public static extern bool UnhookWindowsHookEx(int hookHandle);
        \[DllImport("user32.dll")\]
        public static extern int GetKeyState(int vKey);
    
        // Fields, constants, and structures used by the keyboard hook.
        int hookHandle = 0;
        HookProc cb = null;
    
        public const int WH\_KEYBOARD = 2;
    
        public const int HC\_ACTION = 0;
        public const int HC\_NOREMOVE = 3;
    
        public const int VK\_CONTROL = 0x11;
        public const int VK\_LWIN = 0x5B;
        public const int VK\_RWIN = 0x5C;
        public const int VK\_APPS = 0x5D;
        public const int VK\_LSHIFT = 0xA0;
        public const int VK\_RSHIFT = 0xA1;
        public const int VK\_LCONTROL = 0xA2;
        public const int VK\_RCONTROL = 0xA3;
    
    C 1 Reply Last reply
    0
    • W Wheels012

      Good morning. I am trying to detect the music button events from the keyboard, but have been unsuccessful so far. I have the following code:

      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 System.Runtime.InteropServices;
      using System.IO;
      using System.Collections;

      namespace KPlayer
      {
      public partial class Form1 : Form
      {
      WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
      WMPLib.IWMPPlaylist pl;
      //WMPLib.IWMPPlaylistArray plItems;
      WMPLib.IWMPMedia media;

          // Required designer variable
          //private System.ComponentModel.Container components = null;
      
          ArrayList ar = new ArrayList();
          int intCount = 0;      
          List<int> numbers = new List<int>();
        
          float currentSize = 10;
          string currentFont = "DS-Digital";
      
          public Form1()
          {
              // Required for Windows Form Designer support
              InitializeComponent();
          }
         
          \[STAThread\]
          // Declare Windows API calls used to access Windows hooks
          \[DllImport("user32.dll")\]
      
          public static extern int SetWindowsHookEx(int hookType,
                                                     HookProc callback,
                                                     int instance,
                                                     int threadID);
          \[DllImport("user32.dll")\]
          public static extern int CallNextHookEx(int hookHandle, int code,
                                                  int wparam, int lparam);
          \[DllImport("user32.dll")\]
          public static extern bool UnhookWindowsHookEx(int hookHandle);
          \[DllImport("user32.dll")\]
          public static extern int GetKeyState(int vKey);
      
          // Fields, constants, and structures used by the keyboard hook.
          int hookHandle = 0;
          HookProc cb = null;
      
          public const int WH\_KEYBOARD = 2;
      
          public const int HC\_ACTION = 0;
          public const int HC\_NOREMOVE = 3;
      
          public const int VK\_CONTROL = 0x11;
          public const int VK\_LWIN = 0x5B;
          public const int VK\_RWIN = 0x5C;
          public const int VK\_APPS = 0x5D;
          public const int VK\_LSHIFT = 0xA0;
          public const int VK\_RSHIFT = 0xA1;
          public const int VK\_LCONTROL = 0xA2;
          public const int VK\_RCONTROL = 0xA3;
      
      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      Can you please post your call to SetWindowsHookEx. What hook are you using WH_KEYBOARD or WH_KEYBOARD_LL? Because the function delegate called by the hook (Proc in your case) can be called from threads/processes beyond your scope, you should not do this: textBox1.Text += "Browser Back key caught" + Environment.NewLine; Use an invoke or for testing purposes just trace.

      Greetings Covean

      W 1 Reply Last reply
      0
      • C Covean

        Can you please post your call to SetWindowsHookEx. What hook are you using WH_KEYBOARD or WH_KEYBOARD_LL? Because the function delegate called by the hook (Proc in your case) can be called from threads/processes beyond your scope, you should not do this: textBox1.Text += "Browser Back key caught" + Environment.NewLine; Use an invoke or for testing purposes just trace.

        Greetings Covean

        W Offline
        W Offline
        Wheels012
        wrote on last edited by
        #3

        Hi Covean. That is all the "hook" code I have. I am not exactly clear what you are asking for. WHEELS

        C 1 Reply Last reply
        0
        • W Wheels012

          Hi Covean. That is all the "hook" code I have. I am not exactly clear what you are asking for. WHEELS

          C Offline
          C Offline
          Covean
          wrote on last edited by
          #4

          Ok so this will be the problem. ^^ Without calling SetWindowsHookEx how should the system know what you wanna hook? :-D Somewhere you should call (for example at form init): IntPtr hHook = SetWindowsHookEx(WH_KEYBOARD or WH_KEYBOARD_LL(for low-level hook), Proc (delegate to your hook proc), Process.GetCurrentProcess().Handle, 0); if(hHook == IntPtr.Zero) { // hook failed } At destruction time call UnhookWindowsHookEx(hHook);

          Greetings Covean

          W 1 Reply Last reply
          0
          • C Covean

            Ok so this will be the problem. ^^ Without calling SetWindowsHookEx how should the system know what you wanna hook? :-D Somewhere you should call (for example at form init): IntPtr hHook = SetWindowsHookEx(WH_KEYBOARD or WH_KEYBOARD_LL(for low-level hook), Proc (delegate to your hook proc), Process.GetCurrentProcess().Handle, 0); if(hHook == IntPtr.Zero) { // hook failed } At destruction time call UnhookWindowsHookEx(hHook);

            Greetings Covean

            W Offline
            W Offline
            Wheels012
            wrote on last edited by
            #5

            Now I understand. I have the following:

                // Click event handlers for button1 and button2.
                private void button1\_Click(object sender, System.EventArgs e)
                {
                    // Set the keyboard hook.
                    if (hookHandle == 0)
                    {
                        cb = new HookProc(Proc);
                        hookHandle = SetWindowsHookEx(WH\_KEYBOARD, cb, 0,
                                                      appdomain.GetCurrentThreadId( ));
                    }
                    else
                    {
                        textBox1.Text += "Hook already set" + Environment.NewLine;
                    }
                    textBox1.Text += "Start: " + hookHandle + Environment.NewLine;
                }
            
                private void button2\_Click(object sender, System.EventArgs e)
                {
                    // Unhook the keyboard hook.
                    textBox1.Text += "End: " + UnhookWindowsHookEx(hookHandle) +
                                     Environment.NewLine;
                    hookHandle = 0;
                }
            

            I am coming up with an error on this line: hookHandle = SetWindowsHookEx(WH_KEYBOARD, cb, 0, appdomain.GetCurrentThreadId( )); appdomain doesn't exist in the current context. WHEELS

            C 1 Reply Last reply
            0
            • W Wheels012

              Now I understand. I have the following:

                  // Click event handlers for button1 and button2.
                  private void button1\_Click(object sender, System.EventArgs e)
                  {
                      // Set the keyboard hook.
                      if (hookHandle == 0)
                      {
                          cb = new HookProc(Proc);
                          hookHandle = SetWindowsHookEx(WH\_KEYBOARD, cb, 0,
                                                        appdomain.GetCurrentThreadId( ));
                      }
                      else
                      {
                          textBox1.Text += "Hook already set" + Environment.NewLine;
                      }
                      textBox1.Text += "Start: " + hookHandle + Environment.NewLine;
                  }
              
                  private void button2\_Click(object sender, System.EventArgs e)
                  {
                      // Unhook the keyboard hook.
                      textBox1.Text += "End: " + UnhookWindowsHookEx(hookHandle) +
                                       Environment.NewLine;
                      hookHandle = 0;
                  }
              

              I am coming up with an error on this line: hookHandle = SetWindowsHookEx(WH_KEYBOARD, cb, 0, appdomain.GetCurrentThreadId( )); appdomain doesn't exist in the current context. WHEELS

              C Offline
              C Offline
              Covean
              wrote on last edited by
              #6

              If you want to hook more than the input to your application, you should set dwThreadId to 0 and just set the instance handle (3rd parameter) to Process.GetCurrentProcess().Handle. Btw: appdomain does not exists its called AppDomain.

              Greetings Covean

              W 1 Reply Last reply
              0
              • C Covean

                If you want to hook more than the input to your application, you should set dwThreadId to 0 and just set the instance handle (3rd parameter) to Process.GetCurrentProcess().Handle. Btw: appdomain does not exists its called AppDomain.

                Greetings Covean

                W Offline
                W Offline
                Wheels012
                wrote on last edited by
                #7

                Still get same error when I capitalize appDomain. WHEELS

                S 1 Reply Last reply
                0
                • W Wheels012

                  Still get same error when I capitalize appDomain. WHEELS

                  S Offline
                  S Offline
                  Saksida Bojan
                  wrote on last edited by
                  #8

                  Wheels012 wrote:

                  Still get same error when I capitalize appDomain. WHEELS

                  Still wrong. There are 2 letters to capatilaze: "AppDomain"

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups