Recent

Author Topic: Translate c++ code  (Read 336 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1998
    • Lazarus and Free Pascal italian community
Translate c++ code
« on: December 11, 2019, 10:11:31 am »
Hi guys, since the mouseandkeyinput package does not work on Cocoa, I am thinking of writing something that settles everything. I found the best way to write it in C ++. I enclose an example. But I don't know how to do the equivalent in lazarus. Can anyone help me translate the example code written in C ++? Thanks
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 1998
    • Lazarus and Free Pascal italian community
Re: Translate c++ code
« Reply #1 on: December 12, 2019, 10:59:40 am »
No suggestion?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

MarkMLl

  • Sr. Member
  • ****
  • Posts: 295
Re: Translate c++ code
« Reply #2 on: December 12, 2019, 11:15:16 am »
You've shown us some header files. What are you trying to do and why isn't it working?

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

xinyiman

  • Hero Member
  • *****
  • Posts: 1998
    • Lazarus and Free Pascal italian community
Re: Translate c++ code
« Reply #3 on: December 12, 2019, 11:18:37 am »
It is a source in C ++. It works properly. I want to turn it into object pascal code.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

del

  • Full Member
  • ***
  • Posts: 144
Re: Translate c++ code
« Reply #4 on: December 12, 2019, 11:21:27 am »
My Linux Archive Manager identifies that as Objective C source code. I went straight from C to C++. Let's look at this:

Code: C  [Select]
  1. // File:
  2. // click.m
  3. //
  4. // Compile with:
  5. // gcc -o click click.m -framework ApplicationServices -framework Foundation
  6. //
  7. // Usage:
  8. // ./click
  9. // At the given coordinates it will click and release.
  10.  
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include "virtual_keycodes.h"
  15. #import <Foundation/Foundation.h>
  16. #import <ApplicationServices/ApplicationServices.h>
  17.  
  18. //gcc -o click click.m -framework ApplicationServices -fwork Foundation
  19.  
  20. int main(int argc, char *argv[]) {
  21. /*
  22.  
  23. kCGEventNull
  24.  
  25. Specifies a null event.
  26. kCGEventLeftMouseDown
  27.  
  28. Specifies a mouse down event with the left button.
  29. kCGEventLeftMouseUp
  30.  
  31. Specifies a mouse up event with the left button.
  32. kCGEventRightMouseDown
  33.  
  34. Specifies a mouse down event with the right button.
  35. kCGEventRightMouseUp
  36.  
  37. Specifies a mouse up event with the right button.
  38. kCGEventMouseMoved
  39.  
  40. Specifies a mouse moved event.
  41. kCGEventLeftMouseDragged
  42.  
  43. Specifies a mouse drag event with the left button down.
  44. kCGEventRightMouseDragged
  45.  
  46. Specifies a mouse drag event with the right button down.
  47. kCGEventKeyDown
  48.  
  49. Specifies a key down event.
  50. kCGEventKeyUp
  51.  
  52. Specifies a key up event.
  53. kCGEventFlagsChanged
  54.  
  55. Specifies a key changed event for a modifier or status key.
  56. kCGEventScrollWheel
  57.  
  58. Specifies a scroll wheel moved event.
  59. kCGEventTabletPointer
  60.  
  61. Specifies a tablet pointer event.
  62. kCGEventTabletProximity
  63.  
  64. Specifies a tablet proximity event.
  65. kCGEventOtherMouseDown
  66.  
  67. Specifies a mouse down event with one of buttons 2-31.
  68. kCGEventOtherMouseUp
  69.  
  70. Specifies a mouse up event with one of buttons 2-31.
  71. kCGEventOtherMouseDragged
  72.  
  73. Specifies a mouse drag event with one of buttons 2-31 down.
  74. kCGEventTapDisabledByTimeout
  75.  
  76. Specifies an event indicating the event tap is disabled because of timeout.
  77. kCGEventTapDisabledByUserInput
  78.  
  79. Specifies an event indicating the event tap is disabled because of user input.
  80.  
  81. */
  82.  
  83. //write W LAZARUS
  84. CGEventFlags flags_shift = kCGEventFlagMaskShift;
  85. CGEventRef ev;
  86. CGEventSourceRef source = CGEventSourceCreate (kCGEventSourceStateCombinedSessionState);
  87.  
  88. //press down            
  89. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_W, true);    
  90. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  91. CGEventPost(kCGHIDEventTap,ev);
  92. CFRelease(ev);  
  93.  
  94. //press up                                  
  95. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_W, false);                      
  96. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  97. CGEventPost(kCGHIDEventTap,ev);
  98. CFRelease(ev);
  99.  
  100. //press down            
  101. ev = CGEventCreateKeyboardEvent (source, kVK_Space, true);    
  102. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  103. CGEventPost(kCGHIDEventTap,ev);
  104. CFRelease(ev);  
  105.  
  106. //press up                                  
  107. ev = CGEventCreateKeyboardEvent (source, kVK_Space, false);                      
  108. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  109. CGEventPost(kCGHIDEventTap,ev);
  110. CFRelease(ev);
  111.  
  112. //press down            
  113. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_L, true);    
  114. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  115. CGEventPost(kCGHIDEventTap,ev);
  116. CFRelease(ev);  
  117.  
  118. //press up                                  
  119. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_L, false);                      
  120. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  121. CGEventPost(kCGHIDEventTap,ev);
  122. CFRelease(ev);
  123.  
  124. //press down
  125. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_A, true);    
  126. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  127. CGEventPost(kCGHIDEventTap,ev);
  128. CFRelease(ev);
  129.  
  130. //press up                                  
  131. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_A, false);                      
  132. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  133. CGEventPost(kCGHIDEventTap,ev);
  134. CFRelease(ev);
  135.  
  136. //press down
  137. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_Z, true);    
  138. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  139. CGEventPost(kCGHIDEventTap,ev);
  140. CFRelease(ev);
  141.  
  142. //press up                                  
  143. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_Z, false);                      
  144. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  145. CGEventPost(kCGHIDEventTap,ev);
  146. CFRelease(ev);
  147.  
  148. //press down
  149. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_A, true);    
  150. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  151. CGEventPost(kCGHIDEventTap,ev);
  152. CFRelease(ev);    
  153.  
  154. //press up                                  
  155. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_A, false);                      
  156. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  157. CGEventPost(kCGHIDEventTap,ev);
  158. CFRelease(ev);
  159.  
  160. //press down
  161. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_R, true);    
  162. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  163. CGEventPost(kCGHIDEventTap,ev);
  164. CFRelease(ev);
  165.  
  166. //press up                                  
  167. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_R, false);                      
  168. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  169. CGEventPost(kCGHIDEventTap,ev);
  170. CFRelease(ev);
  171.  
  172. //press down
  173. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_U, true);    
  174. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  175. CGEventPost(kCGHIDEventTap,ev);
  176. CFRelease(ev);
  177.  
  178. //press up                                  
  179. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_U, false);                      
  180. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  181. CGEventPost(kCGHIDEventTap,ev);
  182. CFRelease(ev);
  183.  
  184. //press down
  185. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_S, true);    
  186. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  187. CGEventPost(kCGHIDEventTap,ev);
  188. CFRelease(ev);
  189.  
  190. //press up                                  
  191. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_S, false);                      
  192. CGEventSetFlags(ev,flags_shift | CGEventGetFlags(ev)); //combine flags                        
  193. CGEventPost(kCGHIDEventTap,ev);
  194. CFRelease(ev);            
  195.  
  196. CFRelease(source);
  197.  
  198.  
  199.  
  200. //mouse move and click example
  201. CGEventRef move1 = CGEventCreateMouseEvent(
  202.         NULL, kCGEventMouseMoved,
  203.         CGPointMake(200, 200),
  204.         kCGMouseButtonLeft // ignored
  205. );
  206.  
  207. // Left button down at 250x250
  208. CGEventRef click1_down = CGEventCreateMouseEvent(
  209.         NULL, kCGEventLeftMouseDown,
  210.         CGPointMake(250, 250),
  211.         kCGMouseButtonLeft
  212. );
  213.  
  214. // Left button down at 250x250
  215. CGEventRef clickr2_down = CGEventCreateMouseEvent(
  216.         NULL, kCGEventRightMouseDown,
  217.         CGPointMake(300, 300),
  218.         kCGMouseButtonRight
  219. );
  220.  
  221.  
  222. CGEventPost(kCGHIDEventTap, move1);
  223. sleep(1);
  224. CGEventPost(kCGHIDEventTap, click1_down);
  225. sleep(1);
  226. CGEventPost(kCGHIDEventTap, clickr2_down);
  227. sleep(1);
  228.  
  229. CFRelease(click1_down);
  230. CFRelease(clickr2_down);
  231. CFRelease(move1);
  232.  
  233. //shift + command + 4
  234. //CGEventFlags flags_shift = kCGEventFlagMaskShift;
  235. CGEventFlags flags_command = kCGEventFlagMaskCommand;
  236. //CGEventRef ev;
  237. //CGEventSourceRef source = CGEventSourceCreate (kCGEventSourceStateCombinedSessionState);
  238.  
  239. //press down 4          
  240. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_4, true);    
  241. CGEventSetFlags(ev,flags_shift | flags_command | CGEventGetFlags(ev)); //combine flags                        
  242. CGEventPost(kCGHIDEventTap,ev);
  243. CFRelease(ev);              
  244.  
  245. //press up 4                                
  246. ev = CGEventCreateKeyboardEvent (source, kVK_ANSI_4, false);                      
  247. CGEventSetFlags(ev,flags_shift | flags_command | CGEventGetFlags(ev)); //combine flags                        
  248. CGEventPost(kCGHIDEventTap,ev);
  249. CFRelease(ev);              
  250.  
  251. CFRelease(source);
  252.  
  253. return 0;
  254. }
  255.  

Nope. Can't help you. Looks really alien. Neither fish nor fowl.

xinyiman

  • Hero Member
  • *****
  • Posts: 1998
    • Lazarus and Free Pascal italian community
Re: Translate c++ code
« Reply #5 on: December 12, 2019, 11:54:41 am »
This is code that only works on mac os x (cocoa widget)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1