Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to catch keystrokes in VBA?

Status
Not open for further replies.

hstijnen

Programmer
Nov 13, 2002
172
NL
Hi,

is it possible to catch keystrokes in VBA? I would like to catch the event a Word user does Enter and then test if the paragraph is empty.

 
Hi
I could be wrong but as far as I'm aware there is no event to capture keystrokes within a document (or even the application(?)). It's possible on a userform and it's associated controls using KeyUp, KeyDown or KeyPress but they aren't available to the doc.

I'll keep an eye on this to see if I'm proven wrong!

Happy Friday

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
I'm gonna keep an eye too.

Could you explain why you want to do this? It would be easy to run code that checks for empty paragraphs. However, you are asking for real time checking. That means checking every keystroke. It can not otherwise, as whatever you could come up with has to check each keystroke to see if it IS an Enter key first, and THEN go back and check if the last paragraph is empty.

This seem a rather heavy load. The routine would have to remain in memory the whole time.

I suppose it may be possible, although like Loomah I do not specifically know of a way to do so at the document level. Why is it needed to be real time. Again, a routine to check for empty paragraphs is easy, but it would have to be called, not reside in memory.

Why are you needing this?

Gerry
 
Why am I needing this....

It's my utmost last hopeles trial (see my post "Intelligence in MS-Word", about ending the bullet style) to prevent my users, after closing a bulleted list, to enter paragraph after paragraph not in correct style without their being conscious of it. I'm used to programming in C++Builder where it's easy and not heavy load to check all keystrokes. I hoped the same could be the case in VBA.

The best thing I now think to do is setting the next paragraph style of MyBulletListStyle to Body Text. If it is to be a next item in the list the user has to do it with alt-L (shortkey for MyBulletListStyle). I will follow your suggestion to define a ListEnd style to create the space after the list.

Thanks again.
Henk
 
I have to say the effort is trying to force something that - in your own words - is a factor of "conscious".

OK, I remember that thread.

You have a MyBulletList style. It should have a Style for following paragraph set as MyBulletList. Why the same? Because there are very few instances where a bulletted list has one bullet. So you want the user to be able to just hit Enter and keep going adding the next item in the list. Right?

So the issue is....what do you do when you get to the end of the list?

You have a MyBulletListEnd style. It is formatted exactly like MyBulletList (but with perhaps an extra space after to make it look better). However, ITS style for following paragraph is Bodytext - or whatever it is you want.

Then....what a surprise...you raise some consciousness, maybe even show the use that it looks better when it done properly. Another way is to make a shortcut key for the End style, something simple, Alt-E perhaps. badda-bing, badda-bam.

If I may suggest...and perhaps my suggestions are not worth much...but it would be far better to have the change (the shortcut key) at the end of the list. Because there is only ONE end. If the user has to use a shortcut key for every item in the list, if they have a 15 items in the list that is 15 extra keystrokes. It is going to, rightly, annoy them.

The issue is ending the list properly. Deal with it there. And you are absolutely correct...it IS a matter of conscious. However, trapping keystrokes in VBA will not do that.

Gerry
 
Hi,

By occasion I've found my answer in
the key code is:
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _
KeyCategory:=wdKeyCategoryMacro, Command:="Normal.styles.EnterKeyMacro"

and in Normal.styles.EnterKeyMacro I can place the code I want: if one hits Enter in a bulleted list and the paragraph is empty, then I only change the style of the paragraph. In all other cases I do a TypeParagraph.

Cheers,

Henk
 
can anyone help me with getting my backspace keey to work and also causing a textbox to get focus from the click of the mouse?
 
What do you mean by "getting my backspace key to work"?

what about textbox: is it anyway disabled?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top