Link to home
Start Free TrialLog in
Avatar of simpath
simpathFlag for Australia

asked on

Keyboard + SendInput + erratic results + opens Menus

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

Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

simpath,

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?
;-)

JeffCoachman
untitled.JPG
Are you using ^A or CTRL+A? CTRL = ^, Shift = + etc.

http://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm
Avatar of simpath

ASKER

Hi guys

Many thanks for the response.

I am trying to select the left hand portion of a word with the keystroke combination
CTL + SHIFT + LEFT.

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

'Activate
Call Program_API_Module1.SetForegroundWindow(Me.Hwnd_TextControl)


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


'Select TO START OF WORD (= CTL + SHIFT + LEFT)
Call Sendkey("^+{LEFT}", 0) 'LOC (= select word, left of Curser).
DoEvents


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

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

Clipboard.Clear

'*******

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

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
Loop
DoEvents


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


'REPAINT
Call Program_API_RePaintWindow.RepaintWindow_Hwnd(Hwnd_TextControl)
DoEvents

'RETURN Value
GetWordBeforeCaret_SENDKEY = Shortcut

'Activate
Call Program_API_Module1.SetForegroundWindow(Hwnd_TextControl)

End Function

'********************


SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
simpath,

<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?

JeffCoachman
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial