Retrieve text from a Microsoft Hot Key control

Just starting out? Need help? Post your questions and find answers here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Retrieve text from a Microsoft Hot Key control

Post by netmaestro »

I've been through everything I can find in the PSDK on this, and so far no luck. I can get the word containing the hotkey value with #HKM_GETHOTKEY, but what I want is the text in the window. It's sitting right there, it has to be retrievable. I've tried some #EM_ messages, GetWindowText_, looking for child windows, (doesn't seem to have any) but I'm coming up empty. Can anyone solve this one?

Image
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Is this cheating?

Code: Select all

Procedure.s KeyName(Key.b)
  Ret.s
  Select Key
    Case #PB_Shortcut_Add
      Ret = "Add"
    Case #PB_Shortcut_Back
      Ret = "Backspace"
    Case #PB_Shortcut_Tab
      Ret = "Tab"
    Case #PB_Shortcut_Return
      Ret = "Enter"
    Case #PB_Shortcut_Menu
      Ret = "Context menu"
    Case #PB_Shortcut_Pause 
      Ret = "Pause"
    Case #PB_Shortcut_Print
      Ret = "Print Screen"
    Case #PB_Shortcut_Capital
      Ret = "Caps Lock"
    Case #PB_Shortcut_Escape
      Ret = "Escape"
    Case #PB_Shortcut_Space
      Ret = "Space"
    Case #PB_Shortcut_Prior
      Ret = "Page Up"
    Case #PB_Shortcut_Next
      Ret = "Page Down"
    Case #PB_Shortcut_End
      Ret = "End"
    Case #PB_Shortcut_Home
      Ret = "Home"
    Case #PB_Shortcut_Left
      Ret = "Left arrow"
    Case #PB_Shortcut_Up 
      Ret = "Up arrow"
    Case #PB_Shortcut_Right
      Ret = "Right arrow"
    Case #PB_Shortcut_Down 
      Ret = "Down arrow"
    Case #PB_Shortcut_Select
      Ret = "Select"
    Case #PB_Shortcut_Insert
      Ret = "Insert"
    Case #PB_Shortcut_Delete
      Ret = "Delete"
    Default
      If Key >= #PB_Shortcut_F1 And Key <= #PB_Shortcut_F24
        Ret = "F" + Str(Key - #PB_Shortcut_F1 + 1)
      Else
        Ret = Chr(Key)
      EndIf
  EndSelect
  ProcedureReturn Ret
EndProcedure

Procedure.l ShortcutHasCtrl(Sc.l)
  If (Sc / #PB_Shortcut_Control) % 2
    ProcedureReturn 1
  EndIf
  ProcedureReturn 0
EndProcedure

Procedure.l ShortcutHasAlt(Sc.l)
  If Sc / #PB_Shortcut_Alt
    ProcedureReturn 1
  EndIf
  ProcedureReturn 0
EndProcedure

Procedure.l ShortcutHasShift(Sc.l)
  If (Sc / #PB_Shortcut_Shift) % 2
    ProcedureReturn 1
  EndIf
  ProcedureReturn 0
EndProcedure

Procedure.s ShortcutName(Sc.l)
  Name.s
  If ShortcutHasCtrl(Sc)
    Name + "Ctrl+"
  EndIf
  If ShortcutHasAlt(Sc)
    Name + "Alt+"
  EndIf
  If ShortcutHasShift(Sc.l)
    Name + "Shift+"
  EndIf
  Name + KeyName(Sc)
  ProcedureReturn Name
EndProcedure
Last edited by Trond on Mon Oct 09, 2006 8:01 pm, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

That's actually the kind of codeblock I'm desperately trying to avoid, and if I have to resort to it I may just give up and make people look in the control to see the value. The text is sitting right in that control, it just wouldn't make sense for there not to be some kind of message that'll get at it.
BERESHEIT
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

Code: Select all

Procedure.s GetKeyName(ascii)
  Define name$=Space(255) ; Prepare string to hold name of the desired key.
  GetKeyNameText_(MapVirtualKey_(ascii,0)*$10000,name$,255) ; Get key name.
  ProcedureReturn Trim(name$)
EndProcedure
Debug GetKeyName(#VK_TAB)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Bonne_den_kule, that's an excellent solution. I'll be able to use it to accomplish my task nicely. I was trying to avoid writing a long, all-encompassing parsing routine that would inevitably have holes in it, but employing that will keep it nice and short and accurate. Thanks a million for posting, it's a day-saver!
Last edited by netmaestro on Tue Oct 10, 2006 8:59 am, edited 1 time in total.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

hello,

is it possible to retrieve extended keys name (on multimedia keyboard) such as 'web' key, 'mail' key, 'previous', 'next', ... ) ?

I tried the GetKeyName() procedures on those extended keys and it doesn't work.

Code: Select all

Procedure.s GetKeyName(ascii) 
  
  Protected name.s = Space(255) 
  
  GetKeyNameText_(MapVirtualKey_(ascii, 0) * $10000, name, 255) 
  
  ProcedureReturn Trim(name) 
  
EndProcedure 

Enumeration #VK_RMENU + 1 ; #VK pour Windows 2000/XP 
  #VK_BROWSER_BACK 
  #VK_BROWSER_FORWARD 
  #VK_BROWSER_REFRESH 
  #VK_BROWSER_STOP 
  #VK_BROWSER_SEARCH 
  #VK_BROWSER_FAVORITES 
  #VK_BROWSER_HOME 
  #VK_VOLUME_MUTE 
  #VK_VOLUME_DOWN 
  #VK_VOLUME_UP 
  #VK_MEDIA_NEXT_TRACK 
  #VK_MEDIA_PREV_TRACK 
  #VK_MEDIA_STOP 
  #VK_MEDIA_PLAY_PAUSE 
  #VK_LAUNCH_MAIL 
  #VK_LAUNCH_MEDIA_SELECT 
  #VK_LAUNCH_APP1 
  #VK_LAUNCH_APP2 
EndEnumeration 

If OpenWindow(0, 100, 100, 300, 200, "Multimedia", #PB_Window_SystemMenu) 
  
  Repeat 
    
    Select WaitWindowEvent() 
      
      Case #WM_CLOSE 
        Break 
        
      Case #WM_KEYDOWN 
        
        If EventlParam() & $1000000 
          
          Debug "Extended-key" 
          
          Select EventwParam() 
            Case #VK_BROWSER_BACK:        Debug "#VK_BROWSER_BACK" 
            Case #VK_BROWSER_FORWARD:     Debug "#VK_BROWSER_FORWARD" 
            Case #VK_BROWSER_REFRESH:     Debug "#VK_BROWSER_REFRESH" 
            Case #VK_BROWSER_STOP:        Debug "#VK_BROWSER_STOP" 
            Case #VK_BROWSER_SEARCH:      Debug "#VK_BROWSER_SEARCH" 
            Case #VK_BROWSER_FAVORITES:   Debug "#VK_BROWSER_FAVORITES" 
            Case #VK_BROWSER_HOME:        Debug "#VK_BROWSER_HOME" 
            Case #VK_VOLUME_MUTE:         Debug "#VK_VOLUME_MUTE" 
            Case #VK_VOLUME_DOWN:         Debug "#VK_VOLUME_DOWN" 
            Case #VK_VOLUME_UP:           Debug "#VK_VOLUME_UP" 
            Case #VK_MEDIA_NEXT_TRACK:    Debug "#VK_MEDIA_NEXT_TRACK" 
            Case #VK_MEDIA_PREV_TRACK:    Debug "#VK_MEDIA_PREV_TRACK" 
            Case #VK_MEDIA_STOP:          Debug "#VK_MEDIA_STOP" 
            Case #VK_MEDIA_PLAY_PAUSE:    Debug "#VK_MEDIA_PLAY_PAUSE" 
            Case #VK_LAUNCH_MAIL:         Debug "#VK_LAUNCH_MAIL" 
            Case #VK_LAUNCH_MEDIA_SELECT: Debug "#VK_LAUNCH_MEDIA_SELECT" 
            Case #VK_LAUNCH_APP1:         Debug "#VK_LAUNCH_APP1" 
            Case #VK_LAUNCH_APP2:         Debug "#VK_LAUNCH_APP2" 
          EndSelect 
          
        Else 
          
          Debug "Standard-key: " + GetKeyName(EventwParam()) 
          
        EndIf 
        
    EndSelect 
    
  ForEver 
  
EndIf
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
KarLKoX
Enthusiast
Enthusiast
Posts: 681
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Post by KarLKoX »

Code: Select all

Enumeration
  #APPCOMMAND_BROWSER_BACKWARD        =   1
  #APPCOMMAND_BROWSER_FORWARD
  #APPCOMMAND_BROWSER_REFRESH
  #APPCOMMAND_BROWSER_STOP
  #APPCOMMAND_BROWSER_SEARCH
  #APPCOMMAND_BROWSER_FAVORITES
  #APPCOMMAND_BROWSER_HOME
  #APPCOMMAND_VOLUME_MUTE
  #APPCOMMAND_VOLUME_DOWN
  #APPCOMMAND_VOLUME_UP
  #APPCOMMAND_MEDIA_NEXTTRACK
  #APPCOMMAND_MEDIA_PREVIOUSTRACK
  #APPCOMMAND_MEDIA_STOP
  #APPCOMMAND_MEDIA_PLAY_PAUSE
  #APPCOMMAND_LAUNCH_MAIL
  #APPCOMMAND_LAUNCH_MEDIA_SELECT
  #APPCOMMAND_LAUNCH_APP1
  #APPCOMMAND_LAUNCH_APP2
  #APPCOMMAND_BASS_DOWN
  #APPCOMMAND_BASS_BOOST
  #APPCOMMAND_BASS_UP
  #APPCOMMAND_TREBLE_DOWN
  #APPCOMMAND_TREBLE_UP
  
;#if(_WIN32_WINNT >= 0x0501)
  #APPCOMMAND_MICROPHONE_VOLUME_MUTE
  #APPCOMMAND_MICROPHONE_VOLUME_DOWN
  #APPCOMMAND_MICROPHONE_VOLUME_UP
  #APPCOMMAND_HELP
  #APPCOMMAND_FIND
  #APPCOMMAND_NEW
  #APPCOMMAND_OPEN
  #APPCOMMAND_CLOSE
  #APPCOMMAND_SAVE
  #APPCOMMAND_PRINT
  #APPCOMMAND_UNDO
  #APPCOMMAND_REDO
  #APPCOMMAND_COPY
  #APPCOMMAND_CUT
  #APPCOMMAND_PASTE
  #APPCOMMAND_REPLY_TO_MAIL
  #APPCOMMAND_FORWARD_MAIL
  #APPCOMMAND_SEND_MAIL
  #APPCOMMAND_SPELL_CHECK
  #APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE
  #APPCOMMAND_MIC_ON_OFF_TOGGLE
  #APPCOMMAND_CORRECTION_LIST
  #APPCOMMAND_MEDIA_PLAY
  #APPCOMMAND_MEDIA_PAUSE
  #APPCOMMAND_MEDIA_RECORD
  #APPCOMMAND_MEDIA_FAST_FORWARD
  #APPCOMMAND_MEDIA_REWIND
  #APPCOMMAND_MEDIA_CHANNEL_UP
  #APPCOMMAND_MEDIA_CHANNEL_DOWN
EndEnumeration

Macro LOWORD(Value)
  Value & $FFFF
EndMacro

Macro HIWORD(Value)
  (Value >> 16) & $FFFF
EndMacro 

#FAPPCOMMAND_MASK  = $F000
Macro GET_APPCOMMAND_LPARAM(lParam)
  HIWORD(lParam) & ~#FAPPCOMMAND_MASK
EndMacro

Procedure WndProc(WindowID, message, wParam, lParam)
Protected result.l, cmd.l

  result = #PB_ProcessPureBasicEvents
  
  If message = #WM_APPCOMMAND
    cmd = GET_APPCOMMAND_LPARAM(lParam)
    Debug "cmd = " + Str(cmd)
  EndIf
  
  ProcedureReturn Result
EndProcedure

If OpenWindow(0,0,0,320,240,"Multimedia Keyboard Extension")
  SetWindowCallback(@WndProc())
  Repeat : event = WaitWindowEvent() : Until event = #PB_Event_CloseWindow
EndIf
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
Post Reply