change ESC function temporarly while using VBA

change ESC function temporarly while using VBA

jalal.mousa
Advocate Advocate
592 Views
3 Replies
Message 1 of 4

change ESC function temporarly while using VBA

jalal.mousa
Advocate
Advocate

Is there a way to change ESC function from exit command  to Close (polyline) while using vba and then change it back to normal

can i access CUI under Keyboard Shortcuts ?

 

0 Likes
Accepted solutions (1)
593 Views
3 Replies
Replies (3)
Message 2 of 4

Ed__Jobe
Mentor
Mentor
Accepted solution

That won't work, I don't think the {ESC} key is defined in the CUI. Using Windows api's, you can error trap for it and work around it, sort of skip over it. First you have to add the api's to your module, then in your sub, you need to add error trapping. The code below is copied from one of my sub's. Start with this, then set a breakpoint in your code. Start debugging and test by hitting {ESC}. Execution should jump to line 29. Step through to line 30. Hover  your mouse over the Err variable and see what the error code is. That's what you need in the Case statement at line 31. Then the code will test to see if the {ESC} was used and if it was, resume at the next line.

 

 

 

Option Explicit

'GetAsyncKeyState constants
Private Enum enGetAsyncKeyState
    VK_ESCAPE = &H1B
    VK_LBUTTON = &H1
    VK_RBUTTON = &H2
    VK_RETURN = &HD
    VK_SPACE = &H20
End Enum

Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As enGetAsyncKeyState) As Integer

Public Sub ErrorTrapExample()
  On Error GoTo Err_Control
  'Set up undo for this command
  ThisDrawing.StartUndoMark

  'start your process

  'do work


    
Exit_Here:
    ThisDrawing.EndUndoMark
    Exit Sub
     
Err_Control:
    Select Case Err.Number
    Case -2147352567
        If GetAsyncKeyState(VK_ESCAPE) > 0 Then
            Err.Clear
            Resume
        End If
'     Case -2145320928
'       'User input is keyword or..
'       'Right click
'       Err.Clear
'       Resume Exit_Here
    Case Else
        MsgBox Err.Description
        Resume Exit_Here
    End Select
End Sub

 

 

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 4

jalal.mousa
Advocate
Advocate

Hi ED

this was a great solution to work with, not sure if it was clear in my previous post, but i am sending commands from Excel

I had to adjust code to work with my needs

not sure if i did mistake but if statement below was returning value 0 so i adjust it to = 0 so it can close object

'If GetAsyncKeyState(VK_ESCAPE) > 0 Then

 also i adjusted close command to edit last polyline and close it, as close by itself will close AutoCAD

'ThisDrawing.SendCommand "_pedit" & vbCr & "_select" & vbCr & "_LAST" & vbCr & "_CLOSE " & vbCr

 

new ESC function changes become

 

' ESC Function canges
'If GetAsyncKeyState(VK_ESCAPE) = 0 Then
''close command triggered
'closeKeyPressed = True
'ThisDrawing.SendCommand "_pedit" & vbCr & "_select" & vbCr & "_LAST" & vbCr & "_CLOSE " & vbCr

'End If

0 Likes
Message 4 of 4

Ed__Jobe
Mentor
Mentor

Just be aware that SendCommand is asynchronous, i.e. it will run after your vba code has finished. So trying to prevent esc is pointless. To make it work, you would need to use the ActiveX api to modify the pline's properties.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes