Toggling the Insert Key

B

Brian Stigler

I would like to toggle the Insert Key ON when a certain type of form is
diaplayed. I have been able to get the state of the Insert Key but can't
set the state via my class module. I get an error stating Can't Find DLL
entry point for GetKeyboardState in "user32"...

I'm using Office XP "Word 10" and have created the below class module and
program calls... I'm guessing I'm using an outdated library or something???

Any help would be greatly appreciated. NOTE: code was "lifted" from an
example in an EXCEL Programming book by John Walkenbach for toggling the
NUMLOCK key. I just replaced NUMLOCK with INSERTKEY. I want to use the
code in Word not EXCEL...

Thanks,
Brian

'*****My Calls*******
Private Sub Toggle_Insert_Key_ON()
Dim InsertKey As New InsertKeyClass
InsertKey.Value = True
End Sub
'
Private Sub Toggle_Insert_Key_OFF()
Dim InsertKey As New InsertKeyClass
InsertKey.Value = False
End Sub

'***CLASS MODULE - InsertKeyClass***
Private Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Private kbArray As KeyboardBytes
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long)
As Long
Private Declare Function GetKeyBoardState Lib "user32" (kbArray As
KeyboardBytes) As Long
Private Declare Function SetKeyBoardState Lib "user32" (kbArray As
KeyboardBytes) As Long
'
Property Get Value() As Boolean
Value = GetKeyState(vbKeyInsert) And 1 = 1 ' &H2D
End Property
'
Property Let Value(boolVal As Boolean)
GetKeyBoardState kbArray
kbArray.kbByte(vbKeyInsert) = Abs(boolVal)
SetKeyBoardState kbArray
End Property
 
J

Jay Freedman

Hi, Brian,

The immediate cause of the error is having a capital B that should be lower
case in the declaration of GetKeyboardState. The mechanism that locates DLL
entry points is case-sensitive.

BUT... there are two problems. One is that toggling the keyboard state of
the Insert key will *NOT* change whether or not Word overtypes. Once you get
your macro working, you'll find that out...

The other objection is that playing with these API calls is like
hand-building a new pair of shoes every time you want to walk down to the
pub. John needed to do this to affect the NUMLOCK key because VBA doesn't
have a method for dealing with that key. In contrast, Overtype (not Insert)
is an option that you can set in VBA with one line!

So rip out all that junk, and put this into a regular module in your
template:

Sub AutoNew()
Options.Overtype = False
End Sub

Sub AutoOpen()
Options.Overtype = False
End Sub

If you want to be extra nice to your users, you can add code to each routine
to save the value of Options.Overtype that the user has when they start your
document (you can use the PrivateProfileString method to write to the
registry or to a settings file). Then write a DocumentBeforeClose event
procedure (see http://www.mvps.org/word/FAQs/MacrosVBA/PseudoAutoMacros.htm)
to restore the saved value when the user closes the document.
 
D

DeeJee

I would like to toggle the Insert Key ON when a certain type of form is
diaplayed.

You can't toggle ligths of NumLock and CapsLock with GetKeyState
GetKeyboardState and SetKeyboardState Api.
So for the Insert Key maybe you can get or set the state,
but if it really changes? I don't think so.
If you need to turn lights on and off and setting the state, you do it with
SendInput Api in XP, and this works but I did'nt try it for the Insert key.
You need this constants:
Const VK_CAPITAL = &H14
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
(I found the Insert key constant in VB Api viewer)
Const VK_INSERT = &H2D

Following info came from a VBsite, but I don't really know the URL anymore.

We can call SetKeyboardState, GetKeyboardState, and GetKeyState
from user32.dll to set states for things like the Caps Lock and Num Lock.
These API methods, however, do not change the keyboard state lights.
If we want to synthesize a keystroke and change
the keyboard state lights, we can call the user32.dll keybd_event method
in conjunction with MapVirtualKey. This will have the effect of changing
the keyboard state and lights.
Finally, one version of MSDN suggests that these methods have been
deprecated in favor of SendInput for Windows NT, 2000, and XP.
 
D

DeeJee

The SendInput API works for Num, Caps en ScrolLock for setting state
and lights. And I tried now the Insert key with positive result.
All tested in WordXP.
But I agree that Options.Overtype is a better solution for the Insert key.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top