Loop until a certain key is pressed

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,916
Office Version
  1. 365
Platform
  1. Windows
CTRL-BREAK (that will actually interrupt and stop your code, but I doubt that is what you are after).

I don't know if that is possible because usually your code is either pausing as it is waiting for you to input something (like through an Input Box or Message Box), or it is running constantly and not looking for an input. I never heard of code running and looking for an input at the same time.

Maybe if you explain exactly what you are trying to do in detail, there is another solution we can offer.
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,077
I actually want it to loop until the left mouse button is clicked

i was thinking if I create a global variable (blnCancel) then set it with the right click event then use DoEvents and an exit statement in the loop

im just wicked lost and don't know where to begin.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,916
Office Version
  1. 365
Platform
  1. Windows
But what is your loop actually doing?
How are you determining when you want to stop?

I am just trying to understand the big picture here to see if there is another way to "skin this cat"...
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,077

ADVERTISEMENT

alright well, I think I have away around the loop then.

example: I have a loop and it gets my cursors position if my cursor position is equal to the same position a number of times it gets out of the loop. I now want it so that if my cursor moves again it goes back into the loop.


is there a way to recognize keystrokes or movements in a workbook.

if I hit the letter E I want it to do something (non loop here just in general I hit the letter E). Same with cursor movement above. I can get out of a loop but how can i get back in
 

MorganO

Active Member
Joined
Nov 21, 2006
Messages
483
I am using a keystroke check API in an excel program I am writing. Here is the code:

Code:
'This is how I get Keyboard Input
Declare Function GetAsyncKeyState Lib "User32.dll" (ByVal vKey As Long) As Long

'-- Constant Values --
Const VK_CONTROL = &H11, VK_LEFT = &H25, VK_UP = &H26, VK_RIGHT = &H27, VK_DOWN = &H28
Const VK_0 = &H30, VK_1 = &H31, VK_2 = &H32, VK_3 = &H33, VK_4 = &H34
Const VK_5 = &H35, VK_6 = &H36, VK_7 = &H37, VK_8 = &H38, VK_9 = &H39
Const VK_A = &H41, VK_B = &H42, VK_C = &H43, VK_D = &H44, VK_E = &H45
Const VK_F = &H46, VK_G = &H47, VK_H = &H48, VK_I = &H49, VK_J = &H4A
Const VK_K = &H4B, VK_L = &H4C, VK_M = &H4D, VK_N = &H4E, VK_O = &H4F
Const VK_P = &H50, VK_Q = &H51, VK_R = &H52, VK_S = &H53, VK_T = &H54
Const VK_U = &H55, VK_V = &H56, VK_W = &H57, VK_X = &H58, VK_Y = &H59, VK_Z = &H5A
Const VK_ENTER = &HD

'Here is how you use the function
If GetAsyncKeyState(VK_ENTER) <> 0 Then DoSomething
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,077

ADVERTISEMENT

thanx dude!,

Exactly what I was looking for
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,077
so i added the following:

Const VK_ENTER = &HD, MOUSEEVENTF_LEFTDOWN = &H2, MOUSEEVENTF_MOVE = &H1, MOUSEEVENTF_RIGHTDOWN = &H8


when i use MOUSEEVENTF_LEFTDOWN it only works for my righ click works fine.

so i figured maybe it was the opposite but when i use MOUSEEVENTF_RIGHTDOWN nothing at all happens

any ideas

EDIT; I want to do stuff when left mouse button is clicked. it works great for right (when I use left for some reason)..
 

MorganO

Active Member
Joined
Nov 21, 2006
Messages
483
I am not sure where you pulled you key code constants from, but they do not map correctly to the key codes that this API uses. See this link:

http://msdn2.microsoft.com/en-us/library/ms645540.aspx

The codes for:

Right-mouse button down = &H2
Left-mouse button down = &H1

I also noticed that you are trying to use this API to detect mouse movement and from what I read, this API is not designed for that. You will need to research an alternative. I do not have this information, sorry.

Take care.

Owen
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,077
well it works?

is there a reason why I shouldn't use it to detect right/left clicks?

seems to work 100% fine.

thanks for the new constrant codes
 

Watch MrExcel Video

Forum statistics

Threads
1,122,518
Messages
5,596,621
Members
414,081
Latest member
Subaru_Steve

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top