Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Why does sendkeys seem to loop

When a user types the space bar, I want 1 space, but if they type
<Ctrl+space I want to insert 5 spaces where the cursor is in the text box
(sort of simulating a tab, but I want to keep the tab key for moving focus
from this control to the next).

If the message box line is commented out, spaces keep getting added in what
seems like an endless loop. If the message box line us uncommented and
allowed to execute, then 5 spaces are added along with the original space
making a total of 6 (1 space too many). If I change the keycode = 32 to
keycode = 188 (a comma, I think) then the correct number of spaces is added
and there is no ",".

How can I fix this?
Thanks,
Geoff

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 32 And Shift = 2 Then
'MsgBox "Both the <ctrl key and <spc key were pressed"
SendKeys " ", False 'inserts 5 spaces
End If
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Why does sendkeys seem to loop

Geoff,

Try this without SendKeys

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 32 And Shift = 2 Then
KeyCode = 32
TextBox1.Text = TextBox1.Text & " "
End If
End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Geoff Martin" wrote in message
...
When a user types the space bar, I want 1 space, but if they type
<Ctrl+space I want to insert 5 spaces where the cursor is in the text box
(sort of simulating a tab, but I want to keep the tab key for moving focus
from this control to the next).

If the message box line is commented out, spaces keep getting added in

what
seems like an endless loop. If the message box line us uncommented and
allowed to execute, then 5 spaces are added along with the original space
making a total of 6 (1 space too many). If I change the keycode = 32 to
keycode = 188 (a comma, I think) then the correct number of spaces is

added
and there is no ",".

How can I fix this?
Thanks,
Geoff

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 32 And Shift = 2 Then
'MsgBox "Both the <ctrl key and <spc key were pressed"
SendKeys " ", False 'inserts 5 spaces
End If
End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Why does sendkeys seem to loop

Thanks. Is there a way to make that also insert the spacing at the cursor's
location? E.g., if it's in the middle of a word, then insert 5 spaces there?

Thanks again,
Geoff

--
--take out the nope to reach me--
"Bob Phillips" wrote in message
...
Geoff,

Try this without SendKeys

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 32 And Shift = 2 Then
KeyCode = 32
TextBox1.Text = TextBox1.Text & " "
End If
End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Geoff Martin" wrote in message
...
When a user types the space bar, I want 1 space, but if they type
<Ctrl+space I want to insert 5 spaces where the cursor is in the text

box
(sort of simulating a tab, but I want to keep the tab key for moving

focus
from this control to the next).

If the message box line is commented out, spaces keep getting added in

what
seems like an endless loop. If the message box line us uncommented and
allowed to execute, then 5 spaces are added along with the original

space
making a total of 6 (1 space too many). If I change the keycode = 32 to
keycode = 188 (a comma, I think) then the correct number of spaces is

added
and there is no ",".

How can I fix this?
Thanks,
Geoff

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,

ByVal
Shift As Integer)
If KeyCode = 32 And Shift = 2 Then
'MsgBox "Both the <ctrl key and <spc key were pressed"
SendKeys " ", False 'inserts 5 spaces
End If
End Sub






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Why does sendkeys seem to loop

Hi,

Just an idea: if its possible for the cell to get focus (not sure if
this is limited to forms?), you could then use the Instr function to
search out and replace a specific character with the five spaces.
Alternatively, could you combine cell.parse with a merge function?
Seems a long way round.....

Just shootin the breeze on this one, if I can get some code together,
will post it.

Cheers


---
Message posted from http://www.ExcelForum.com/

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Why does sendkeys seem to loop

Thanks for the info. I'm using text boxes in a UserForm, and didn't want to
have to save data to worksheet if I can avoid it. I'm sure there's some
convention I haven't followed. I think your suggestion will definitely come
in handy when I start modifying a "nonsense word" generator for generating
words for beginning reading practice. Thanks for the info.

--
--take out the nope to reach me--
"Joseph " wrote in message
...
Hi,

Just an idea: if its possible for the cell to get focus (not sure if
this is limited to forms?), you could then use the Instr function to
search out and replace a specific character with the five spaces.
Alternatively, could you combine cell.parse with a merge function?
Seems a long way round.....

Just shootin the breeze on this one, if I can get some code together,
will post it.

Cheers


---
Message posted from http://www.ExcelForum.com/





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Why does sendkeys seem to loop

Got this from www.VisualBasicForum.com in response to my question. Thanks to
Herilane from London, England. Seems to work to fit my needs AND takes care
of the looping regardless of where the cursor insertion point is.

Thanks to everyone who had suggestions,
Geoff

Sendkeys simulates key presses, so the event triggers itself, which puts you
in a loop.
There's no Application.EnableEvents equivalent for userforms, so you'll have
to create one yourself - like this, for example:


VB:
----------------------------------------------------------------------------
----
Public RunEvents As Boolean

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 32 And Shift = 2 And RunEvents = True Then
RunEvents = False
SendKeys " "
DoEvents
RunEvents = True
End If
End Sub

Private Sub UserForm_Initialize()
RunEvents = True
End Sub
----------------------------------------------------------------------------
----


I'm not entirely sure why the DoEvents is necessary, but it seems to be, to
make things happen in the proper order so to say.

__________________
Please use the [vb] and [/vb] tags.

"Geoff Martin" wrote in message
...
When a user types the space bar, I want 1 space, but if they type
<Ctrl+space I want to insert 5 spaces where the cursor is in the text box
(sort of simulating a tab, but I want to keep the tab key for moving focus
from this control to the next).

If the message box line is commented out, spaces keep getting added in

what
seems like an endless loop. If the message box line us uncommented and
allowed to execute, then 5 spaces are added along with the original space
making a total of 6 (1 space too many). If I change the keycode = 32 to
keycode = 188 (a comma, I think) then the correct number of spaces is

added
and there is no ",".

How can I fix this?
Thanks,
Geoff

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 32 And Shift = 2 Then
'MsgBox "Both the <ctrl key and <spc key were pressed"
SendKeys " ", False 'inserts 5 spaces
End If
End Sub




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sendkeys string hawki Excel Discussion (Misc queries) 4 May 11th 07 08:30 PM
How Can I Use SendKeys in MAC EXCEL? kenji4861 Excel Programming 1 October 29th 03 07:24 AM
Sendkeys GB[_3_] Excel Programming 0 September 16th 03 10:39 PM
SendKeys Ron de Bruin Excel Programming 3 August 25th 03 10:21 PM
sendkeys mark poole Excel Programming 0 July 11th 03 03:47 PM


All times are GMT +1. The time now is 12:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"