Why doesn't OnKey work?

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I have the following two subs below. From the help files, it's my
understanding that after running sub "setit", I should be able to do a
"Control s" and then sub "doit" will be called. It doesn't work. I have
Excel 2003. Am I misunderstanding the usage of ONKEY or what am I doing
wrong??

Thanks for any help

Sub doit()
Dim aa
aa = aa
End Sub

Sub setit()
Application.OnKey "^s", "doit"
End Sub
 
Sub doit()
Dim aa
aa = aa
MsgBox "In Doit"
End Sub

Sub setit()
Application.OnKey "^s", "doit"
End Sub

Sub Unsetit()
Application.OnKey "^s"
End Sub


worked fine for me.
 
Mike,

I believe the onKey command has to be a continue run mode. You are running
it once expecting a key capture. I believe it needs contstant polling. You
should have the setit command call itself & code in a keystroke to end the
entire process.
 
No, that isn't true.

--
Regards,
Tom Ogilvy


galimi said:
Mike,

I believe the onKey command has to be a continue run mode. You are
running
it once expecting a key capture. I believe it needs contstant polling.
You
should have the setit command call itself & code in a keystroke to end the
entire process.
 
Just an added thought, all the code should be in a general module, not in a
worksheet or the thisworkbook module.
 
Works for me. Try replacing aa=aa in your doit routine with
MsgBox "You pressed [Ctrl]+"

just to verify that it is (or isn't) happening.

I put my code in a regular code module, not in worksheet or workbook
modules, and it worked like a champ. Excel 2003, Windows XP Pro.
Sub CalledRoutine()
MsgBox "You pressed [Ctrl]+"
End Sub
Sub SetItUp()
Application.OnKey "^s", "CalledRoutine"
End Sub
 
I thought that "^s" was normally used in keystoke to save a file.

JLatham said:
Works for me. Try replacing aa=aa in your doit routine with
MsgBox "You pressed [Ctrl]+"

just to verify that it is (or isn't) happening.

I put my code in a regular code module, not in worksheet or workbook
modules, and it worked like a champ. Excel 2003, Windows XP Pro.
Sub CalledRoutine()
MsgBox "You pressed [Ctrl]+"
End Sub
Sub SetItUp()
Application.OnKey "^s", "CalledRoutine"
End Sub


Mike said:
I have the following two subs below. From the help files, it's my
understanding that after running sub "setit", I should be able to do a
"Control s" and then sub "doit" will be called. It doesn't work. I have
Excel 2003. Am I misunderstanding the usage of ONKEY or what am I doing
wrong??

Thanks for any help

Sub doit()
Dim aa
aa = aa
End Sub

Sub setit()
Application.OnKey "^s", "doit"
End Sub
 
With a little bit more experimenting, it appears that it works if I'm typing
into a WorkSheet but I was hoping that it would work when a Userform had the
focus.

Hopefull thinking, huh!!
 
but I was hoping that it would work when a Userform had the focus.

Too bad you didn't think to mention that in your original post.
 
You're correct, that is the normal definition. But using the .OnKey function
will change that definition! To set it back to the default, you would use
Application.OnKey "^s"
without the second parameter.

JLGWhiz said:
I thought that "^s" was normally used in keystoke to save a file.

JLatham said:
Works for me. Try replacing aa=aa in your doit routine with
MsgBox "You pressed [Ctrl]+"

just to verify that it is (or isn't) happening.

I put my code in a regular code module, not in worksheet or workbook
modules, and it worked like a champ. Excel 2003, Windows XP Pro.
Sub CalledRoutine()
MsgBox "You pressed [Ctrl]+"
End Sub
Sub SetItUp()
Application.OnKey "^s", "CalledRoutine"
End Sub


Mike said:
I have the following two subs below. From the help files, it's my
understanding that after running sub "setit", I should be able to do a
"Control s" and then sub "doit" will be called. It doesn't work. I have
Excel 2003. Am I misunderstanding the usage of ONKEY or what am I doing
wrong??

Thanks for any help

Sub doit()
Dim aa
aa = aa
End Sub

Sub setit()
Application.OnKey "^s", "doit"
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top