We help IT Professionals succeed at work.

Using VBAs SendKeys in a Multiline Text Box {HOME} doesn't go to the beginning of the line

PeteyBgood
PeteyBgood asked
on
280 Views
Last Modified: 2013-11-28
Instead it goes to the beginning of the field like Ctl -Home. I'm trying to do some fancy editing with shortcut keys, but didn't get far. Hitting the HOME key does as expected but
     SendKeys "{HOME}"
does not ... what gives? And how do I work around it. This is driving me nuts.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Well that seems to work, so I'll work with it a while. I still don't understand why SendKeys' behavior is different from a macro that sending the keys from VBA..  Wait a sec, I'm using ^Y to do a {HOME+{END} (select line) but because  the control key is down when the keys are sent, it thinks it's getting a ^{HOME}^+{END}. THAT'S why it selects the whole document, so if I move the code to the KeyUp event... I'll be back
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Also be aware that Sendkeys is notorious fickle and prone to failure. Most professional applications avoid it like the plague.
Jeffrey CoachmanMIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012

Commented:
PeteyBgood,

" Hitting the HOME key does as expected..."
If hitting the Home key works, then why even bother with Sendkeys or an autokeys macro?

As far as "why" sendkeys does not work, is probably becaues it is looking for Line feeds or Carrige Returns to signify the start of a line.
If your text has none, it simply goes to the start of the field.

JeffCoachman

Author

Commented:
Hitting the 'Home' key would be easier, but then that's not what automation is all about. I need to define procs that can do things like "Delete to EOL" or determine if I'm on the first or last lines. Since Access doesn't provide full access to the text box's info like "Current Line", "Line Count" , word wrap info etc. I'm trying to work around it with SendKeys. I've played with it and you're right, the macro approach works. I found something in the dox about how only macros can cancel keyboard events, so my DoCmd.CancelEvents in the KeyDown event handler did nothing which explains why {HOME} was processed as ^{HOME} when ^{whatever } is pressed.

Author

Commented:
Oh yeah, and it doen't have to be an autokeys macro, SendKeys in any macro can be called from VBA without the current keyboard state interfering.

Author

Commented:
IN FACT You don't even have to send any keys, using "" as the SendKeys argument in the macro does the same thing (you can't just leave the argument blank you have to deliberately tell it to send nothing. This seems a little kludgey, but I've been guilty of lots worse. I consider this matter closed, and you'll have your points by day's end, unless somebody else can up with something better.So to tell ^Y to blank a line without remove the CRLF:
Private Sub ..... _KeyDown (...)
   Select Case KeyCode
      Case vbKeyY
         DoCmd.RunMacro "Skeys.FlushKbd"
         SendKeys ( "{HOME}+{END}{DEL}")
         ...............
where Skeys is my macro group and FlushKbd is a macro that does SendKeys "",False
 

Author

Commented:
I think I expect too much out of software that says it ca do it all.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.