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?
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
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.
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.
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
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?