Detecting KeyPress

Detecting KeyPress

Anonymous
Not applicable
1,592 Views
7 Replies
Message 1 of 8

Detecting KeyPress

Anonymous
Not applicable
Can I intercept keypresses (e.g. ESC), the user may press while a form
is hidden and a method (e.g. .GetPoint()), is being executed?

Thanks
Tom
0 Likes
1,593 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
I think that you will find the answer that you need in the thread from
21.01.2003 "Trapping an ESC key and terminating the program" which was
started by me. If it is already gone from your news reader, you can find it
through Google News search by typing
Trapping an ESC group:autodesk.autocad.customization.vba
in the search box.
You can contact me if you need any additional information.

Regards, Marko.


"TBerry" wrote in message news:3E3EF072.4010209@vhb.com...
> Can I intercept keypresses (e.g. ESC), the user may press while a form
> is hidden and a method (e.g. .GetPoint()), is being executed?
>
> Thanks
> Tom
>
0 Likes
Message 3 of 8

Anonymous
Not applicable
Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
'This is a constant that represents the "ESC" key and is the
'Key value that will be used for the above function
Public Const VK_ESCAPE = &H1B

"TBerry" wrote in message news:3E3EF072.4010209@vhb.com...
> Can I intercept keypresses (e.g. ESC), the user may press while a form
> is hidden and a method (e.g. .GetPoint()), is being executed?
>
> Thanks
> Tom
>
0 Likes
Message 4 of 8

Anonymous
Not applicable
Marko,



I searched for this in Google but all references to it relate to Excel (Application.EnableCancelKey
= xlErrorHandler). What did you use for AutoCAD?



Thank you

Tom



Marko Rogic wrote:


I think that you will find the answer that you need in the thread from
21.01.2003 "Trapping an ESC key and terminating the program" which was
started by me. If it is already gone from your news reader, you can find it
through Google News search by typing
Trapping an ESC group:autodesk.autocad.customization.vba
in the search box.
You can contact me if you need any additional information.

Regards, Marko.


"TBerry" <tberry@vhb.com> wrote in message news:3E3EF072.4010209@vhb.com...


Can I intercept keypresses (e.g. ESC), the user may press while a form
is hidden and a method (e.g. .GetPoint()), is being executed?

Thanks
Tom








0 Likes
Message 5 of 8

Anonymous
Not applicable
LHGO,



I do very little VB/VBA programming (Delphi & Java 98% of time), so I'm
at a loss for using this. I declared it in a module and then tried to use
the function. When I run the VBA code, the error message "Compile error:
Ambiguous name detected: GetAsyncKeyState". How do I implement this function?



Thanks

Tom



LHGO wrote:


Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
'This is a constant that represents the "ESC" key and is the
'Key value that will be used for the above function
Public Const VK_ESCAPE = &H1B

"TBerry" <tberry@vhb.com> wrote in message news:3E3EF072.4010209@vhb.com...


Can I intercept keypresses (e.g. ESC), the user may press while a form
is hidden and a method (e.g. .GetPoint()), is being executed?

Thanks
Tom








0 Likes
Message 6 of 8

Anonymous
Not applicable
From
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarvbtips/
html/msdn_msdn27.asp

GetAsyncKeyState returns a non-zero value if the specified key or button is
currently pressed. It will also return a non-zero value if the key or button
was pressed since the last call to this function. The CONSTANT.TXT file
contains a list of the virtual key constants.


I found the above link by doing a google search on "Declare Function
GetAsyncKeyState". There are probably other useful links from this same
search.

Here's the thread Marko was talking about (long link):
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=16FD9B7E
8385F3F1587C9F58BB801AB9%40in.WebX.maYIadrTaRb&rnum=3&prev=/groups%3Fq%3Desc
%2Bkey%2Bgroup:autodesk.autocad.customization.vba%26hl%3Den%26lr%3D%26ie%3DU
TF-8%26oe%3DUTF-8%26selm%3D16FD9B7E8385F3F1587C9F58BB801AB9%2540in.WebX.maYI
adrTaRb%26rnum%3D3


I got to this by clicking the Groups tab at Google, searching for "autocad
customization" as a quick way to get to this newsgroup, and then doing a
search on "esc key". There's a very helpful radio button to limit the
search to this newsgroup. There were several other threads on ESC key, they
may also be useful to you.

Good luck,
James
0 Likes
Message 7 of 8

Anonymous
Not applicable
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Const VK_ESCAPE = &H1B
Private Const VK_LBUTTON = &H1


Select Case Err.Number
Case -2147352567
If GetAsyncKeyState(VK_ESCAPE) And &H8000 > 0 Then
Err.Clear
Resume Exit_Here
ElseIf GetAsyncKeyState(VK_LBUTTON) > 0 Then
Err.Clear
Resume
Else
Err.Clear
Resume
End If
Case Else
Call ErrorMsgBox(Err)
Resume Exit_Here
End Select
0 Likes
Message 8 of 8

Anonymous
Not applicable
Hi Tom, this is copied from above mentioned thread.
Hope that it helps.

    Marko.

 

------------


Gary, the reason, why the code that you find on
this ng doesn't work "out of the box", is that this are usually just pieces of
the code and normally you have to add a few pieces by yourself for the code
to come to life. Try the following.

 

1. Open VBA window under an AutoCAD and add a new
UserForm.

2. Then add standard Module.

3. Go to the standard Module and copy to it the
following lines of code from the example:

 

        Option
Explicit

        Public
Declare Function GetAsyncKeyState Lib "user32" _
   
    (ByVal vKey As Long) As Integer

        Public Const
VK_ESCAPE = &H1B
        Public Const
VK_LBUTTON = &H1

 

4. Then go to UserForm and add a Command Button and
name it cmdOK.

5. Switch to the Code view of the UserForm and add
the following function from the example:

 

        Public
Function EntSel(strPrmt As String) As AcadEntity
   
        Dim objTemp As
AcadEntity
            Dim
objUtil As AcadUtility
       
    Dim varPnt As Variant
   
        Dim varCancel As
Variant
            On Error
GoTo Err_Control
            Set
objUtil = ThisDrawing.Utility
       
    objUtil.GetEntity objTemp, varPnt,
strPrmt
            Set EntSel =
objTemp

       
Exit_Here:
            Exit
Function
       
Err_Control:
            Select
Case Err.Number
          
     Case
-2147352567
           
    varCancel =
ThisDrawing.GetVariable("LASTPROMPT")
        
       If InStr(1, varCancel, "*Cancel*") <> 0
Then
         
          If GetAsyncKeyState(VK_ESCAPE)
And 8000 > 0
Then
            
          
Err.Clear
            
           Resume
Exit_Here
            
           ElseIf
GetAsyncKeyState(VK_LBUTTON) > 0
Then
            
          
Err.Clear
        
              
Resume
         
          End
If
          
        
Else
            
       'Missed the pick, send them
back!
            
       Resume
   
            End
If
       
        Case
Else
         
      MsgBox
Err.Description
           
    Resume Exit_Here
       
    End Select
        End
Function

 

6. Go back to the Object view of your UserForm and
double click on a command button that you added two steps back. VBA will
automatically switch over to Code window and add initial part of code for your
command button. Add the following lines between the start and end of the sub so
that it looks like this:

 

    Private Sub
cmdOK_Click()

    Dim strPrmt As
String

       
Me.Hide

       
    strPrmt = "Select object"
   
        EntSel (strPrmt)

face=Arial size=2>        Me.Show

    End Sub

 

7. Well that's it basically, now your code should
work. If you still don't understand something it's most probably because I'm
also quite unfamiliar with VBA. Good luck.

------------
0 Likes