PDA

View Full Version : [SOLVED] Can you clear ALL .OnKey assignments at once?



Paul_Hossler
05-09-2014, 07:59 AM
Is there an easy way to clear all .OnKey assignments without having to loop through a possilbe combinations of the shift keys, special keys, and letter keys?

This seems to work OK, but I don't like all the hard coding. So I was wondering if there was way to just release all at once



Option Explicit


Sub ClearAllOnKeys()

Dim iSpecial As Long, iShift As Long, iLetter As Long, iFunction As Long
Dim aSpecialKeys As Variant, aShiftKeys As Variant

aSpecialKeys = Array( _
"BS", "CAPSLOCK", "CLEAR", "DEL", "DOWN", "END", "ENTER", "~", "ESC", "HELP", "HOME", "INSERT", _
"LEFT", "NUMLOCK", "PGDN", "PGUP", "RETURN", "RIGHT", "SCROLLLOCK", "TAB", "UP")

aShiftKeys = Array(vbNullString, "+", "^", "%", "^%", "^+", "%+", "^%+")

For iShift = LBound(aShiftKeys) To UBound(aShiftKeys)

For iSpecial = LBound(aSpecialKeys) To UBound(aSpecialKeys)
Application.OnKey aShiftKeys(iShift) & "{" & aSpecialKeys(iSpecial) & "}"
Next iSpecial

For iFunction = 1 To 15
Application.OnKey aShiftKeys(iShift) & "{F" & iFunction & "}"
Next iFunction


For iLetter = Asc("a") To Asc("z") '97 to 122
Application.OnKey aShiftKeys(iShift) & Chr(iLetter)
Next iLetter
Next iShift

End Sub

Kenneth Hobs
05-09-2014, 01:32 PM
I guess the easy way would be to poke each one into storage like the registry, text file, or a named range, or even a range. Iterate that rather than a hardcoded array of keys.

For all? http://www.rondebruin.nl/win/s4/win012.htm

Paul_Hossler
05-09-2014, 05:14 PM
I guess the easy way would be to poke each one into storage like the registry, text file, or a named range, or even a range. Iterate that rather than a hardcoded array of keys.

For all? http://www.rondebruin.nl/win/s4/win012.htm

I re-purpose different keys in different add ins and templates on Activate, and reset them on Deactivate.

Sometimes if there's an abend, I'm left with a bad configuration, so I was looking for an 'elegant' way to clear them without the brute force.

I'm pleased that I was able to come up with an approach that was similar to Ron's. I must be improving :beerchug:

Aflatoon
05-12-2014, 05:36 AM
The special keys and F keys all have numeric constant equivalents, so I think you could just use a brute force approach like this:

Sub ClearAllOnKeys()
Dim i As Long, iShift as long
Dim aShiftKeys As Variant

aShiftKeys = Array(vbNullString, "+", "^", "%", "^%", "^+", "%+", "^%+")
On Error Resume Next
For iShift = LBound(aShiftKeys) To UBound(aShiftKeys)

For i = 1 To 255
Application.OnKey aShiftKeys(iShift) & "{" & i & "}"
Application.OnKey aShiftKeys(iShift) & VBA.Chr$(i)
Next i

Next iShift

End Sub