[Solved] Detect if Ctrl key is pressed in a macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
luiz_antoniosp
Posts: 4
Joined: Sat Jul 22, 2023 5:46 pm

[Solved] Detect if Ctrl key is pressed in a macro

Post by luiz_antoniosp »

I need to detect the pressing of the ctrl key, or it can also be combined with the mouse click Ctrl+click , I want to be able to know when to click on a Shape only if the ctrl key is press
Last edited by robleyd on Fri Aug 04, 2023 4:59 am, edited 2 times in total.
Reason: Tagged [Solved]. Add green tick
OpenOffice 3.1
User avatar
Zizi64
Volunteer
Posts: 11373
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to detect pressing the Ctrl key?

Post by Zizi64 »

Is it a Macro related question?

Which application are you using? Draw, Impress,Writer, Calc?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Hagar Delest
Moderator
Posts: 32695
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: How to detect pressing the Ctrl key?

Post by Hagar Delest »

I guess it's about Calc since posted in the Calc forum. But does it mean you have to handle shapes that are in a spreadsheet too?
LibreOffice 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10
luiz_antoniosp
Posts: 4
Joined: Sat Jul 22, 2023 5:46 pm

Re: How to detect pressing the Ctrl key?

Post by luiz_antoniosp »

Zizi64 wrote: Sat Jul 22, 2023 8:07 pm Is it a Macro related question?

Which application are you using? Draw, Impress,Writer, Calc?
what I want:: I want when I click on the shape and simultaneously press the CRTL key and receive a msgbox,
however I am not able to detect only the ctrl , because I can only get it if I combine it with another key and I don't want that
so I ask for help on how to get just the capture of the ctrl or ctrl+click key, to test the code below, run the Sub StartXKeyHandler and assign a shape to the ShapeClickHandler macro

Code: Select all

Global oXKeyHandler As Object
Global bCtrlPressed As Boolean
Global sKeyPressed As String

Sub StartXKeyHandler()
    If IsNull(oXKeyHandler) Then
        oXKeyHandler = CreateUnoListener("XKeyHandler_", "com.sun.star.awt.XKeyHandler")
        ThisComponent.GetCurrentController.AddKeyHandler(oXKeyHandler)
    End If
End Sub

Sub StopXKeyHandler()
    If Not IsNull(oXKeyHandler) Then
        ThisComponent.GetCurrentController.removeKeyHandler(oXKeyHandler)
        oXKeyHandler = Nothing
    End If
End Sub

Sub XKeyHandler_disposing(oEvent)
    Call StopXKeyHandler
End Sub

Function XKeyHandler_keyPressed(oEvent) As Boolean
    XKeyHandler_keyPressed = False  'Não cancele este evento‼

    vCode = oEvent.KeyCode
    Select Case vCode
        ' ... Códigos das teclas ...

        Case Else: vName = "???"
    End Select

    vFunc = Choose(oEvent.KeyFunc + 1, "", "NEW", "OPEN", "SAVE", "SAVEAS", "PRINT", "CLOSE", "QUIT", "CUT", "COPY", "PASTE" _
     , "UNDO", "REDO", "DELETE", "REPEAT", "FIND", "FINDBACKWARD", "PROPERTIES", "FRONT")
    oMod = oEvent.Modifiers  '1=Shift, 2=Ctrl, 4=Alt
    vMod = ""

    If oMod And 1 Then vMod = vMod & "+SHIFT"
    If oMod And 2 Then vMod = vMod & "+CTRL"
    If oMod And 4 Then vMod = vMod & "+ALT"

    vChar = oEvent.KeyChar
    If vChar = "" Then
        vOut = vName
    ElseIf Asc(vChar) > 32 And Asc(vChar) < 176 Then
        vOut = vChar
    Else
        vOut = vName
    End If

    sKeyPressed = vOut & vMod
    MsgBox "Key " & Chr$(34) & sKeyPressed & Chr$(34)
End Function


Function XKeyHandler_keyReleased(oEvent) As Boolean
    XKeyHandler_keyReleased = False  'Don't cancel this‼
End Function

Sub ShapeClickHandler()
    If bCtrlPressed Then
        Dim oShape As Object
        oShape = ThisComponent.CurrentController.getSelection().getByIndex(0)

        If Not IsNull(oShape) Then
            MsgBox "Você clicou na shape: " & oShape.Name
        End If
    Else
        ' Simula a tecla "Space" pressionada
        Dim oKeyEvent As New com.sun.star.awt.KeyEvent
        oKeyEvent.KeyCode = com.sun.star.awt.Key.SPACE
        oKeyEvent.KeyChar = " "
        oKeyEvent.Modifiers = 0  ' Não precisa definir nenhum modificador

        ' Chama o evento XKeyHandler_keyPressed passando o evento personalizado
        XKeyHandler_keyPressed(oKeyEvent)
    End If
End Sub
Last edited by robleyd on Fri Aug 04, 2023 5:01 am, edited 1 time in total.
Reason: Add CODE tags
OpenOffice 3.1
luiz_antoniosp
Posts: 4
Joined: Sat Jul 22, 2023 5:46 pm

Re: How to detect pressing the Ctrl key?

Post by luiz_antoniosp »

Hagar Delest wrote: Sat Jul 22, 2023 9:39 pm I guess it's about Calc since posted in the Calc forum. But does it mean you have to handle shapes that are in a spreadsheet too?
Yes I'm doing this but I want to receive the msgbox if when I click if the ctrl is pressed or not pressed >>

Code: Select all

Sub ShapeClickHandler()
    If bCtrlPressed Then
        Dim oShape As Object
        oShape = ThisComponent.CurrentController.getSelection().getByIndex(0)

        If Not IsNull(oShape) Then
            MsgBox "Você clicou na shape: " & oShape.Name
        End If
    Else
        ' Simula a tecla "Space" pressionada
        Dim oKeyEvent As New com.sun.star.awt.KeyEvent
        oKeyEvent.KeyCode = com.sun.star.awt.Key.SPACE
        oKeyEvent.KeyChar = " "
        oKeyEvent.Modifiers = 0  ' Não precisa definir nenhum modificador

        ' Chama o evento XKeyHandler_keyPressed passando o evento personalizado
        XKeyHandler_keyPressed(oKeyEvent)
    End If
End Sub
Last edited by robleyd on Fri Aug 04, 2023 5:02 am, edited 1 time in total.
Reason: Add CODE tags
OpenOffice 3.1
luiz_antoniosp
Posts: 4
Joined: Sat Jul 22, 2023 5:46 pm

Re: Detect if Ctrl key is pressed in a macro

Post by luiz_antoniosp »

Thank you all, I managed to solve it with the tips offered, thank you very much
OpenOffice 3.1
User avatar
Lupp
Volunteer
Posts: 3556
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Detect if Ctrl key is pressed in a macro

Post by Lupp »

luiz_antoniosp wrote: Fri Aug 04, 2023 4:25 am Thank you all, I managed to solve it with the tips offered, thank you very much
You now surely want to demonstrate your solution running in an example (sheet?) document.
Don't hesitate.
Other users are eager to learn from you.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2803
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Detect if Ctrl key is pressed in a macro

Post by JeJe »

Lupp - I assume it was the Handler's oEvent.modifiers and com.sun.star.awt.KeyModifier.MOD1 (and 2)

https://www.openoffice.org/api/docs/com ... ifier.html
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply