PureBasic Forum
https://www.purebasic.fr/english/

How do I take control of keyboard vol up, down and mute?
https://www.purebasic.fr/english/viewtopic.php?f=5&t=72690
Page 1 of 1

Author:  ozzie [ Sun Apr 21, 2019 10:16 am ]
Post subject:  How do I take control of keyboard vol up, down and mute?

I want to intercept the keyboard volume up, down and mute keys so that I can apply the effect directly in my program, which is particularly important for ASIO audio outputs. I can intercept these keys using a window callback, but I don't seem to be able to force Windows to subsequently ignore the action, even by returning 0 in the callback procedure. I need to be able to do this to avoid both my program and Windows applying volume up/down, which could double the effect (eg when using the default sound device). How can I tell Windows to ignore the volume up, down and mute keys?

Here's my test program:
Code:
Procedure WinCallback(hWnd, uMsg, WParam, LParam)
  Select uMsg
    Case #WM_KEYDOWN
      Debug "$" + Hex(uMsg) + ", #WM_KEYDOWN (" + #WM_KEYDOWN + "), VK=$" + Hex(wparam) + ", lparam=$" + Hex(lparam) + ", prev_key_state=" + Str((lparam & $40000000) >> 30)
      ProcedureReturn 0
    Case #WM_KEYUP
      Debug "$" + Hex(uMsg) + ", #WM_KEYUP (" + #WM_KEYUP + "), VK=$" + Hex(wparam) + ", lparam=$" + Hex(lparam)
      ProcedureReturn 0
    Case #WM_SYSKEYDOWN
      Debug "$" + Hex(uMsg) + ", #WM_SYSKEYDOWN, VK=$" + Hex(wparam) + ", lparam=$" + Hex(lparam) + ", prev_key_state=" + Str((lparam & $40000000) >> 30)
      ProcedureReturn 0
    Case #WM_SYSKEYUP
      Debug "$" + Hex(uMsg) + ", #WM_SYSKEYUP, VK=$" + Hex(wparam) + ", lparam=$" + Hex(lparam)
      ProcedureReturn 0
  EndSelect
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

If OpenWindow(0, 50, 50, 400, 200, "Window Callback Test", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  SetWindowCallback(@WinCallback(),0)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf

Author:  RSBasic [ Sun Apr 21, 2019 10:29 am ]
Post subject:  Re: How do I take control of keyboard vol up, down and mute?

You can use a keyboard hook: https://www.rsbasic.de/aktualisierung/w ... ardHook.pb

Author:  RASHAD [ Sun Apr 21, 2019 11:08 am ]
Post subject:  Re: How do I take control of keyboard vol up, down and mute?

Try
Code:
;*********************************************************
;Mute - Unmute
;*********************************************************
; keybd_event_(#VK_VOLUME_MUTE,0,0,0)
; keybd_event_(#VK_VOLUME_MUTE,0,#KEYEVENTF_KEYUP,0)
;*********************************************************
;Volume Up
;*********************************************************
; keybd_event_(#VK_VOLUME_UP,0,0,0)
; keybd_event_(#VK_VOLUME_UP,0,#KEYEVENTF_KEYUP,0)
;*********************************************************
;Volume Down
;*********************************************************
; keybd_event_(#VK_VOLUME_DOWN,0,0,0)
; keybd_event_(#VK_VOLUME_DOWN,0,#KEYEVENTF_KEYUP,0)
;*********************************************************
; MEDIA_PLAY_PAUSE
;*********************************************************
; keybd_event_(#VK_MEDIA_PLAY_PAUSE,0,0,0)
; keybd_event_(#VK_MEDIA_PLAY_PAUSE,0,#KEYEVENTF_KEYUP,0)
;*********************************************************
;Volume Stop
;*********************************************************
; keybd_event_(#VK_MEDIA_STOP,0,0,0)
; keybd_event_(#VK_MEDIA_STOP,0,#KEYEVENTF_KEYUP,0)


Or
Code:
EnableExplicit

#VK_MEDIA_PREV_TRACK = $B1
#VK_MEDIA_NEXT_TRACK = $B0
#VK_MEDIA_PLAY_PAUSE = $B3
#VK_MEDIA_STOP = $B2
#VK_VOLUME_UP = $AF
#VK_VOLUME_DOWN = $AE
#VK_VOLUME_MUTE = $AD

Procedure SetAsyncKeyState_(Key)
  Protected KeyStroke.INPUT
  KeyStroke\type = 1
  KeyStroke\ki\wVk = Key
  KeyStroke\ki\wScan = 0
  KeyStroke\ki\dwFlags = 0
  KeyStroke\ki\time = 0
  KeyStroke\ki\dwExtraInfo = 0
  ProcedureReturn SendInput_(1, @KeyStroke, SizeOf(INPUT))
EndProcedure

;SetAsyncKeyState_(#VK_MEDIA_PREV_TRACK)
;SetAsyncKeyState_(#VK_MEDIA_NEXT_TRACK)
;SetAsyncKeyState_(#VK_MEDIA_PLAY_PAUSE)
;SetAsyncKeyState_(#VK_MEDIA_STOP)
SetAsyncKeyState_(#VK_VOLUME_UP)
;SetAsyncKeyState_(#VK_VOLUME_DOWN)
;SetAsyncKeyState_(#VK_VOLUME_MUTE)

Author:  ozzie [ Sun Apr 21, 2019 11:54 am ]
Post subject:  Re: How do I take control of keyboard vol up, down and mute?

Many thanks for the quick replies!

RASHAD: it seems your code is for having my program send these keystrokes, not ignore them. But thanks, anyway.

RSBasic: That does the job! Here's my version of the code, compiled and tested using PB 5.70 (I had to remove Structure KBDLLHOOKSTRUCT as PB already has the structure defined):

Code:
EnableExplicit

Procedure.l KeyboardHook(nCode, wParam, *p.KBDLLHOOKSTRUCT)
  If wParam = #WM_KEYDOWN Or wParam = #WM_SYSKEYDOWN
    If *p\vkCode = $AE
      Debug "volume down"
      ProcedureReturn 1
    ElseIf *p\vkCode = $AF
      Debug "volume up"
      ProcedureReturn 1
    ElseIf *p\vkCode = $AD
      ; "volume mute", but ignore on key down due to keyboard auto-repeat
      ProcedureReturn 1
    EndIf
  ElseIf wParam = #WM_KEYUP Or wParam = #WM_SYSKEYUP
    If *p\vkCode = $AD
      Debug "volume mute"
      ProcedureReturn 1
    EndIf
  EndIf
  ProcedureReturn CallNextHookEx_(0, nCode, wParam, *p)
EndProcedure

If OpenWindow(0,0,0,500,250,"Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  TextGadget(1,10,10,200,20,"Taste ''A'' wird gesperrt.",0)
  StringGadget(2,10,30,200,20,"",0)
 
  SetWindowsHookEx_(#WH_KEYBOARD_LL,@KeyboardHook(),GetModuleHandle_(0),0)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/