Results 1 to 3 of 3

Thread: [RESOLVED] Media Keys for Media Keyboards (I know what to look for, but not how)

  1. #1

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4700 x4
    Posts
    1,333

    Resolved [RESOLVED] Media Keys for Media Keyboards (I know what to look for, but not how)

    I would like to implement Media Keyboard support in my application.
    Unfortunately i cannot find out what exactly i'm looking for.

    I need to know how to check for, and react to the Media Key being pressed, "Play" for example.
    I also, do not know what this key is called, therefore even if i could check for it, i couldn't

    The keys i mean are on the Media Keyboards, and use a standard set of commands.
    Keyboard such as the top of this one: http://solbu.com/E-Commerce/zencart/...920-000042.jpg

    EDIT: For example as used in Winamp Media Player along with numerous others.
    EDIT2: Found out how to register hotkeys, or so i think http://www.pinvoke.net/default.aspx/...terHotKey.html But no listing, and still no way of implementing Play, Pause, Stop Next, etc. etc..
    Last edited by thegreatone; May 28th, 2009 at 12:28 PM.
    Zeegnahtuer?

  2. #2

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4700 x4
    Posts
    1,333

    Re: Media Keys for Media Keyboards

    Could be this actually: http://msdn.microsoft.com/en-us/library/ms646275.aspx

    WM_APPCOMMAND's

    Ok, now i found it, i have no idea how to implement it...
    Zeegnahtuer?

  3. #3

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4700 x4
    Posts
    1,333

    Re: Media Keys for Media Keyboards (I know what to look for, but not how)

    Ok, now i have found a working example of disabling or capturing windows keys.


    vb Code:
    1. Option Strict On
    2. Imports System.Runtime.InteropServices
    3.  
    4. Public Class Form1
    5.  
    6.     Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
    7.     Private Const KEYEVENTF_KEYUP As Long = &H2
    8.     Private Const VK_LWIN As Byte = &H5B
    9.     Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    10.     ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    11.  
    12.     Private Const WH_KEYBOARD_LL As Integer = 13
    13.     Private Const WM_KEYUP As Integer = &H101
    14.     Private Shared _proc As LowLevelKeyboardProc = AddressOf HookCallback
    15.     Private Shared _hookID As IntPtr = IntPtr.Zero
    16.  
    17.     Public Declare Auto Function SetWindowsHookEx Lib "user32.dll" ( _
    18.         ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProc, _
    19.         ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
    20.  
    21.     Public Declare Auto Function UnhookWindowsHookEx _
    22.     Lib "user32.dll" (ByVal hhk As IntPtr) As IntPtr
    23.  
    24.     Public Declare Auto Function CallNextHookEx _
    25.     Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, _
    26.                       ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    27.  
    28.     Public Declare Auto Function GetModuleHandle Lib "kernel32.dll" ( _
    29.     ByVal lpModuleName As String) As IntPtr
    30.  
    31.  
    32.     Private Shared Function SetHook( _
    33.         ByVal proc As LowLevelKeyboardProc) As IntPtr
    34.  
    35.         Dim curProcess As Process = Process.GetCurrentProcess()
    36.         Dim curModule As ProcessModule = curProcess.MainModule
    37.  
    38.         Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, _
    39.                 GetModuleHandle(curModule.ModuleName), 0)
    40.  
    41.     End Function
    42.  
    43.     Public Delegate Function LowLevelKeyboardProc( _
    44.         ByVal nCode As Integer, ByVal wParam As IntPtr, _
    45.         ByVal lParam As IntPtr) As IntPtr
    46.  
    47.     Public Shared Function HookCallback( _
    48.         ByVal nCode As Integer, _
    49.         ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    50.  
    51.         If nCode >= 0 And wParam = CType(WM_KEYUP, IntPtr) Then
    52.             Dim vkCode As Keys = CType(Marshal.ReadInt32(lParam), Keys)
    53.             If vkCode = Keys.LWin Or vkCode = Keys.MediaPlayPause Then
    54.                 'keybd_event(CByte(Keys.Zoom), 0, KEYEVENTF_EXTENDEDKEY, 0)
    55.                 MsgBox("LOL WINDOWS KEY MON")
    56.             End If
    57.         End If
    58.  
    59.         Return CallNextHookEx(_hookID, nCode, wParam, lParam)
    60.     End Function
    61.  
    62.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    63.         _hookID = SetHook(_proc)
    64.     End Sub
    65.  
    66.     Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    67.         UnhookWindowsHookEx(_hookID)
    68.     End Sub
    69.  
    70. End Class

    As you can see, i modified it to test, and it works!

    I'll leave this here for anyone who has problems in the future.
    I will learn from this code, but no now, too much Red Bull.

    EDIT: What happened to the VBCODE tags?
    Zeegnahtuer?

Tags for this Thread

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