WFS: Custom Control Accelerator keys

I’m creating a custom control from a canvas which will have a caption. In a Xojo control one just puts a “&” in the caption to support an Accelerator key (i think that is the right windows terminology). For my custom control I would have to support that myself.

Looking in the Windows Functionality Suite I see the class “HotKeyHelperWFS” which I assume will do this. It has the methods:

Function RegisterHotKey(modifiers as Integer, virtualKey as Integer) As Integer
Sub UnregisterHotKey(id as Integer)

But a can’t find a usage example and I don’t know what values to pass for modifiers and virtualKey. Does anyone have a usage example for this class they can share?

Thanks

  • Karen

For windows shortcut you can also do following:

This example will only work when you have created a Notepad shortcut on Desktop

  //The HotKey property sets or retrieves the hotkey combination for a shortcut
  //The valid modifier key-names (which are case insensitive) are: "ALT+", "CTRL+", "SHIFT+", and "EXT+".
  //The valid standard keynames (also case insensitive) are: "A" .. "Z", "0" .. "9", "Back", "Tab", "Clear", "Return", "Escape", "Space", and "Prior".
  
  // Info: http://msdn.microsoft.com/en-us/library/xsy6k3ys%28v=vs.84%29.aspx
  // 
  // As test. Create a notepad shortcut on Desktop
  
  Dim objShell As OLEObject
  Dim objShortcut As OLEObject
  Dim strDesktop, strlink, strPathname As String
  
  strLink = "\otepad.lnk"
  
  objShell = New OLEObject("WScript.Shell")
  strDesktop = objShell.SpecialFolders("Desktop")
  strPathname = strDesktop + strLink
  
  // Creates Hotkey to a known shortcut 
  objShortcut = objShell.CreateShortcut(strPathname) 
  objShortcut.HotKey = "CTRL+ALT+P"
  objShortcut.Save()
  
  msgbox("Hotkey: "+objShortCut.HotKey)

  objShell = Nil
  
exception err as oleexception 
  msgbox err.message

It’s also possible to create a shotcut and then assign a Hotkey. More information can be found on: http://msdn.microsoft.com/en-us/library/xsy6k3ys(v=vs.84).aspx

Thanks, but what I want to do is provide standard Windows UI control behavior for my canvas subclass.

In other words have the letter for the accelerator key (if any) in the caption underline when the alt key is pressed, and have the focus shift to it if that key (with alt) is pressed.

I’m hoping that the WFS class HotKeyHelperWFS will help with that… but there is no documetnation for it that I can find so I am hoping someone here can explain how to use it or provide a simple example.

Hmm…

Maybe the windows functions that is used in the declare for HotKeyHelperWFS can give you some light:

  1. MapVirtualKeyA / MapVirtualKeyW: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646306(v=vs.85).aspx
  2. GetKeyNameTextA / GetKeyNameTextW: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646300(v=vs.85).aspx

Well it looks like the HotKey API in the WFS has nothing to do with accelerator key support and the WFS has no support for them.

While I found the docs for accelerator keys at:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645526(v=vs.85).aspx

I don’t know enough to be able to set up all the declares needed. I wish Xojo had a method to specify an accelerator key for a canvas and raise AltKeyDown , AltKeyUp and Accelerator Pressed events. That is what would be needed to give expected Windows functionality for a custom control made from a canvas.

I suppose an AcceleratorKeyHelperWFS (analogous to the HotKeyHelperWFS) class with the above events could be created to handle this, but I don’t have the requisite background to do it (at least in a reasonable amount of time). I guess since the built-in controls handle this there has not been enough need for someone to add it to the WFS.

  • Karen

I guess no one has managed to hook into OS accelerator functionality for a Custom control made from a canvas?

Has anyone tried?

Thanks,

  • Karen

Unfortunately the Xojo’s Keydown event don’t trigger when you press the “ALT” key.

You might be able to use Microsoft GetKeyState or GetAsyncKeyState.

This example is just to give you a hint how to use it. Add below code to a timer1.action (mode 2) and display output in TextArea1

  // Info: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646301%28v=vs.85%29.aspx
  
  // Virtual key  http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx
  
  // Return value
  // 0 if the key is neither down nor toggled,
  // -127 if the key is down but not toggled,
  // 1 if the key is toggled but up, and
  // -128 if the key is both toggled and down.
  
  
  Soft Declare Function GetKeyState Lib "user32" (keystatus As integer) as short
  
  //TextArea1.AppendText "FirstKey: "+ str(GetKeyState(&h12)) +EndOfLine         // ALT Key
  //TextArea1.AppendText "SecondKey "+ str(GetKeyState(&h42)) +EndOfLine    //B Key
  
  if GetKeyState(&h12) = -127 or GetKeyState(&h12) = -128 Then
    
    if GetKeyState(&h42) = -127 or  GetKeyState(&h42) = -128 Then
      TextArea1.AppendText( "ALT KEY + B KEY DOWN" +EndOfLine)
    End if
    
  end if
 

[quote=85773:@John Hansen]Unfortunately the Xojo’s Keydown event don’t trigger when you press the “ALT” key.
[/quote]

You can detect the alt key press through a timer and Keyboard.AltKey

http://documentation.xojo.com/index.php/Keyboard.AltKey

[quote=85776:@Michel Bujardet]You can detect the alt key press through a timer and Keyboard.AltKey

http://documentation.xojo.com/index.php/Keyboard.AltKey[/quote]

Thanks Michel :slight_smile:

I missed that property

Thanks… I had though about the Keyboard.Async* methods but it’s not practical fro what I wate dto do. What I was hoping for an OS based solution such as a way to register the accelerator key and a callback method with the OS and then get a callback (event) when the alt key and/or the altkey + accelerator key was pressed.

As a workaround, a label has the ability to draw the underscore and call an event when the Accelerator is pressed. But, do you found the way to do it in a canvas?

That was over 5 year ago… I don’t even remember what project that was for!!!

  • karen