Keyboard + SendInput + erratic results + opens Menus

Posted on 2009-05-04
Last Modified: 2013-11-27
I am trying to build a word expansion application in visual basic 6.
(eg "atis" will be replaced with "and there is" )

I am trying to use Sendkeys / SendInput to input keystrokes to an application (eg. Word, Wordpad etc), namely CTL, SHIFT, LEFT ARROW, RIGHT ARROW
in order to select the word the Caret is in (ie. atis), and then do a word replace ie, "atis" becomes "and there is".

The problem is that the Sendinput code (CTL, SHIFT, LEFT, RIGHT etc) causes erratic jumps of the caret, and will often open a menu.

I guess that I need to block user input (efg BlockInput API) during the routine,
but this doesnt seem to work properly

Any Ideas Anybody

Question by:simpath
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 24301120

This functionality is built into Word already.

It's called "AutoCorrect"

Type the characters you want.
Right-Click the characters-->Auto Correct--AutoCorrect Options.

Why re-invent the wheel?

LVL 83

Expert Comment

ID: 24314932
Are you using ^A or CTRL+A? CTRL = ^, Shift = + etc.

Author Comment

ID: 24325805
Hi guys

Many thanks for the response.

I am trying to select the left hand portion of a word with the keystroke combination

Here is some of the code:


Public Function GetWordBeforeCaret_SENDKEY() As String

'NB. ^ Ctrl    ! Alt    + Shift
'****NB. USE the Cliboard to transfer text
'*** NB. SendKey = Use KeySend BAS module
'*** NB. USE THE LOCKWINDOWUPDATE / SETREDRAW FUNCTION to make selection code transparent to USER,

Dim rval As Long
Dim Shortcut As String
Dim SCLen As Integer
Dim i As Integer
On Error Resume Next

'hwnd = Me.Hwnd_TextControl

Call Program_API_Module1.SetForegroundWindow(Me.Hwnd_TextControl)

'Block RePaint
'Call SetRedraw(Hwnd_TextControl, False)
LockWindowUpdate (Hwnd_TextControl) 'WORKS PERFECTLY

Call Sendkey("^+{LEFT}", 0) 'LOC (= select word, left of Curser).

'Copy to Clipboard
SendMessage Hwnd_TextControl, WM_COPY, 0, 0

'Get Shortcut + SCLen
Shortcut = Clipboard.GetText
SCLen = Len(Shortcut)



'Re-set to Caret
Call Sendkey("^+{RIGHT}", 0) 'ROC (= select word, right of Curser).
Call Sendkey("^{LEFT}", 0) 'SOW (= select start of word).

i = 0
Do Until i = SCLen 'Move back to Curser Pos
Call Sendkey("{RIGHT}", 0) 'Move right, one character at a time.
i = i + 1

'Allow Repaint
'Call SetRedraw(Hwnd_TextControl, True)
LockWindowUpdate False  'WORKS PERFECTLY

Call Program_API_RePaintWindow.RepaintWindow_Hwnd(Hwnd_TextControl)

GetWordBeforeCaret_SENDKEY = Shortcut

Call Program_API_Module1.SetForegroundWindow(Hwnd_TextControl)

End Function


Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

LVL 83

Assisted Solution

CodeCruiser earned 250 total points
ID: 24326043
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 24331815

<I am trying to select the left hand portion of a word with the keystroke combination>
...Then I am confused, this is not what you asked for in your original post.

<do a word replace ie, "atis" becomes "and there is".>

This is what my post demonstrates.

Did you look at it?
Did you try it?


Accepted Solution

simpath earned 0 total points
ID: 24335076
Dear CodeCruiser

I changed the 0 to True, and read the links, but it still wasn't the answer.

I think that the problem is a constant repertoir of interference from additional keystrokes. and the timer event, and so I am now working with keyboard hooks,

Dan Applemans Spyware program provides this functionality.


Dear boag2000

Yes I do know about Microsoft Word Autoreplace, and this is a brilliant thing, however I am trying to write my own Word expansion software app, in VB6, and already I have about 50 thousand records.

Many thanks for both contributions.

I will allocate 250 points to CodeCruiser


Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Standards for file storage. 3 36
How Does Quick Books store date / time? 3 109
SignTool Error with Windows 10 5 220
What .NET URL re-routing tool did I use? 2 85
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question