[Solved] Detect if Ctrl key is pressed in a macro
-
- Posts: 4
- Joined: Sat Jul 22, 2023 5:46 pm
[Solved] Detect if Ctrl key is pressed in a macro
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
Reason: Tagged [Solved]. Add green tick
OpenOffice 3.1
Re: How to detect pressing the Ctrl key?
Is it a Macro related question?
Which application are you using? Draw, Impress,Writer, Calc?
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.
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.
- Hagar Delest
- Moderator
- Posts: 32695
- Joined: Sun Oct 07, 2007 9:07 pm
- Location: France
Re: How to detect pressing the Ctrl key?
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
-
- Posts: 4
- Joined: Sat Jul 22, 2023 5:46 pm
Re: How to detect pressing the Ctrl key?
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
Reason: Add CODE tags
OpenOffice 3.1
-
- Posts: 4
- Joined: Sat Jul 22, 2023 5:46 pm
Re: How to detect pressing the Ctrl key?
Yes I'm doing this but I want to receive the msgbox if when I click if the ctrl is pressed or not pressed >>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?
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
Reason: Add CODE tags
OpenOffice 3.1
-
- Posts: 4
- Joined: Sat Jul 22, 2023 5:46 pm
Re: Detect if Ctrl key is pressed in a macro
Thank you all, I managed to solve it with the tips offered, thank you very much
OpenOffice 3.1
Re: Detect if Ctrl key is pressed in a macro
You now surely want to demonstrate your solution running in an example (sheet?) document.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
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
---
Lupp from München
Re: [Solved] Detect if Ctrl key is pressed in a macro
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
https://www.openoffice.org/api/docs/com ... ifier.html
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)