Assign a keyboard shortcut to a cmdbutton in a UF

Hi all

After a lot of research and testing, I can't seem to create a keyboard shortcut to activate a cmdbutton in a UserForm.
I tried different approaches, the last one being this one:

Private Sub UserForm_Initialize()
Me.KeyPreview = True
End Sub

Private Sub UserForm1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyF4 Then
Call cmdButton_1_Click
End If
End Sub

I also tested the command: Application.OnKey, but nothing helps!

Ideally, the shortcut should activate a cmdbutton without the userform having the focal length.
I should point out that I am working on SW 2010. There may be compatibility issues...

If you've ever faced the problem and know the right controls, I'd appreciate it.

Thank you!

Good evening

The only way I've found to make it work is to set the button to Enable = False.
The line at the initialization of the UserForm is not necessary in my opinion.
And I had to change this:

Private Sub UserForm1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

By:

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Hello

Is there anything other than this button on the userform?

Isn't it possible to create the macro dedicated to the automatic execution of the function launched by the said button, and then assign a shortcut to the macro button on the toolbar?

In other words, a keyboard shortcut launches the macro, which launches the function, and closes for the next pressing of the keyboard shortcut keys.

1 Like

Good evening

Thank you for your answers. I'll try tomorrow.
Sylk, yes, I have about twenty buttons on my UF. The idea is to set them all up by assigning them shortcuts.

My UF already has its shortcut but for the sake of efficiency, I would like the twenty or so buttons to be configured.

There are about ten of us who use macros, which are on the server. My first idea was to insert them individually into a tab under SW; Afterwards, I realized how tedious the update would be for everyone if a change in the macro(s) were to occur or if a folder were to change its name or location. The UF seemed to me to be the best solution to ensure fluidity and simplicity in the cases mentioned above.
That being said, if I have missed a " technique " and the UF is not mandatory, I am at your disposal.

Thank you for your answers,

Hello

For the update, it's not that restrictive.
At home, I developed a whole bunch of macros that are launched via pressing an icon next to the save icons, backtracking... SW base base.
As we administer the global configuration and generate a configuration file used by all (sldreg), updating the macro links can be done easily (but if we can leave the paths and methods called fixed, it's always better).
Then for the code update, you just have to find a time when no one has SW open to be able to update the content and deploy new features or bug fixes.
Remember to make macros read-only and lock their content with a password to avoid manipulation by people who know nothing about it (I've already had macros emptied of their content, fortunately I had backups elsewhere).
The idea of @Sylk is a good one, but it requires users to be well informed of the features associated with each keyboard shortcut.

1 Like

Hello @Cyril_f

So for once, the clarity of the associated functions and shortcuts is precisely an asset of this method:
image
An explicit icon and a tooltip with title, shortcut, and description. It is difficult to make it clearer for the user.

For me, nothing beats a personal toolbar with a button for each function linked to a button in the userform. You can leave it open and within reach, or hide it (to make shortcuts " natively integrated" and invisible.
The toolbars are transferable with SW settings, so it's pretty easy to deploy.

The only downside, knowing that the macro closes after each execution of the function, is perhaps if some variable values must persist between 2 functions...

I don't have anything better to offer at the moment.

I'm in sync but from the sight of users at home I can say that even a tooltip goes beyond their " competence ".
Basically, they have to deign to skim over the available icons and not just use SW20xx as we used SW9x (95/98/99)

Hello;
It is possible to create one (or more) complete and configurable toolbar to share on all your Solidworks workstations (via the add-ins):

This toolbar also has the advantage of being able to activate (or not) access to buttons/macros depending on the work environment (if you work in an assembly, it is not necessary to be able to use macros specific to drawings...).
The disadvantage is to have to create personalized and readable icons but it is still surmountable.
Note: The version we use on our workstations is the beta version (called " MyToolbar " at the time). I haven't tested the " Toolbar+ " version offered in the " Cad+ " pack...

1 Like

Hello

Thank you for your answers.
The Toolbar solution has already been tested.


It is good because it allows you to assign individual shortcuts,
However, it requires, if there is a change in the multiple macros, to update the link, for each modified macro, and for each user.
Indeed Cyril_f, for the update, each user would have to have SW closed, to have tested it, it's restrictive. In addition, in addition to the changes made within the macros, there can be changes in the location of files or changes in the names of folders.
I had already tested, as you suggested, Cyril_f to register a .sldreg that would allow a quick update. But this did not prove effective in our situation.
The idea of the USF then was to have only one change to make. Simpler for users.
To go further, the idea of dropping a batch on users' desktops that would allow the updated macro file to be copied and pasted on the server, locally; This is in order to significantly increase the responsiveness of macros.

But I always come back to my problem of shortcuts directly written in the code, no syntax tried seems to work!
Cyril_f, I had written UserForm1 because logically, having 2 USFs in my project, it seemed relevant to me to name it. Am I wrong?

Oh yes, but then what you have to code is a macro to launch the " startUserBrain() " function to their neurons...

1 Like

Hello @N_MS

No. All you have to do is integrate all the functions into a single macro (a common one), and choose the function (called " Method ") targeted for each button.
If you update the macro, as long as you don't change the name of the functions called by the buttons, all the updates will have an impact. The toolbar update at the user's home will only be useful for adding new buttons/functions.

This is the whole point of the toolbar that I propose above.
The latter is to be located on a shared server, the various macros associated with it are in the form of links (location of the macros) and in case of updates or addition of functions, it will be automatically reloaded the next time Solidworks is started for all users.

Otherwise, there is the CustomToolbar utility which also allows you to add a toolbar with shortcuts to macro.
I configure this bar on my computer and then I have a bat file that allows me to propagate on the computer from which the bat file is launched.
A beat file to back up the local files on the server and a file restores the local files by backing up the server.
This allows me to quickly propagate improvements with new macros for all.
Before I used to go through the same toolbar @maclane (but the IT department had blocked the installation of software from a source that was not known enough!)
For the code of the bat file to copy from the network to the local:

Echo Message > log.txt
Echo Message >> log.txt
chcp 65001
Echo "Fermer Solidworks puis appuyer sur entrée"
pause
cd %LOCALAPPDATA%\Visiativ\myCADtools\CustomToolbar
ren "%LOCALAPPDATA%\Visiativ\myCADtools\CustomToolbar" "CustomToolbar save-%date:/=-%" 
xcopy "Z:\XXX\xxx\Solidworks\Configuration\CustomToolbar\Sauvegarde Profil\Profil-SW2023\" "%LOCALAPPDATA%\Visiativ\myCADtools\CustomToolbar" /e /i /c /h /y > "Z:\XXX\xxx\Solidworks\Configuration\CustomToolbar\log.txt

Edit: I just made a development request via Visiativ to have a network backup path and restore via a backup file, for this utility.

2 Likes

… Did they answer you about this development? I had also requested it a few years ago... without answers.

Same thing @Maclane , I made a new request before writing the edit.
By grouping our requests it may pass!
And if @fmarchand, for example , can support the request, that would be even better!
Example of the competing software, which seems great to me on this point:

1 Like