DelphiFeeds.com

  • Dashboard
  • Popular Stories
  • Trending Stories
  • Feeds
  • Login
Trending now

Quick Logger Is A Powerful Enterprise-Grade Asynchronous Logger For Delphi

Learn How To Use C++ Atomic Operations For Windows Development In C++Builder

Easily Create Ultra-Fast C++ Applications With Low-Level libsimdpp Library In C++Builder

Delphi JOSE JWT Is A Powerful JSON Web Token Library For Delphi

Learn How Not To Use Square Brackets in Your Program In This Learn Delphi Video

Easily Communicate With The TI Gas Sensor Platform Using Delphi And C++ Builder

Powerful Video Game Collection Curation Software Is Built In Delphi

Rise of Legions Multiplayer RPG Windows Game Built In Delphi

Cross-Platform 4D Solar System Simulation Is Built In Delphi

Flexible Cross-Platform Open-Source Component Suite For Delphi FireMonkey

Quickly And Easily Hook Delphi And Windows API Functions With The DDetours Library

Discover How to Use C++ Alias Templates For Windows Development In C++Builder

QuickLib Is A Powerful Third Party Library For Delphi Which Can Boost Productivity

Discover How to Clone The Classic Minesweeper Game in Delphi in This Learn Delphi Video

TMS VCL UI Pack v10.5 released

Sending virtual keystrokes on OS X

1
Chris Rolliston Chris Rolliston 6 years ago in OS X 0

Going through my comments backlog I found a question asking how to send virtual keystrokes on OS X. Short answer is to use the CGEvent functions in MacApi.CoreGraphics – in a nutshell,

  1. Call CGEventSourceCreate to allocate a CGEventSourceRef.
  2. For each keystroke, create a CGEventRef by calling CGEventCreateKeyboardEvent; this takes the CGEventSourceRef just allocated plus an OS X virtual key code. For possible values of the latter, see the MacApi.KeyCodes unit, assuming you have a recent Delphi version, otherwise scan the source for KEY_xxx values. Also, note that you will need separate events for first pressing a key down, then unpressing it.
  3. If required, call CGEventSetFlags on a given CGEventRef to assign a modifier key (alt/shift/ctrl/command); in the case of a character, you’ll typically want to call CGEventKeyboardSetUnicodeString as well rather than messing about trying to figure out the correct key code.
  4. Actually perform the event by passing the CGEventRef to CGEventPost, using kCGHIDEventTap for the first parameter.
  5. Clean up by passing each CGEventRef then the CGEventSourceRef to CFRelease.

The following follows these steps to generate key down and key up events for a Cmd+L shortcut:

uses
  System.SysUtils, MacApi.CocoaTypes, MacApi.CoreFoundation, MacApi.CoreGraphics, MacApi.KeyCodes;

procedure PressCmdLShortcut;
var
  EventSourceRef: CGEventSourceRef;
  KeyDownRef, KeyUpRef: CGEventRef;
begin
  KeyDownRef := nil;
  KeyUpRef := nil;
  EventSourceRef := CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
  if EventSourceRef = nil then RaiseLastOSError;
  try
    KeyDownRef := CGEventCreateKeyboardEvent(EventSourceRef, KEY_L, 1); //1 = key down
    if KeyDownRef = nil then RaiseLastOSError;
    CGEventSetFlags(KeyDownRef, kCGEventFlagMaskCommand);
    KeyUpRef := CGEventCreateKeyboardEvent(EventSourceRef, KEY_L, 0);  //0 = key up
    if KeyUpRef = nil then RaiseLastOSError;
    CGEventSetFlags(KeyUpRef, kCGEventFlagMaskCommand);
    CGEventPost(kCGHIDEventTap, KeyDownRef);
    CGEventPost(kCGHIDEventTap, KeyUpRef);
  finally
    if KeyDownRef <> nil then CFRelease(KeyDownRef);
    if KeyUpRef <> nil then CFRelease(KeyUpRef);
    CFRelease(EventSourceRef);
  end;
end;

Alternatively, you can use a little cross platform (Mac/Windows) wrapper interface I’ve written… more on which in my next post!

Trending Stories

  • Quick Logger Is A Powerful Enterprise-Grade Asynchronous Logger For Delphi

  • Learn How To Use C++ Atomic Operations For Windows Development...

  • Easily Create Ultra-Fast C++ Applications With Low-Level libsimdpp Library In...

  • Delphi JOSE JWT Is A Powerful JSON Web Token Library...

  • Learn How Not To Use Square Brackets in Your Program...

Embarcadero GetIt

  • ProDelphi 64-bit & 32-bit. Lite version

    Source code profiler for measuring runtime of 64 and 32 bit applications developed with Delphi. […]

  • TChromeTabs

    TChrome tabs is a comprehensive implementation of Google Chrome's tab system - features include - […]

  • ICS for FMX and VCL

    ICS is a Delphi library composed of many internet components supporting all major protocols and […]

  • ICS for VCL

    ICS is a Delphi library composed of many internet components supporting all major protocols and […]

  • SynEdit for VCL

    SynEdit for Delphi and CBuilder. Syntax highlighting edit control, not based on the Windows common […]

  • Learn Delphi
  • Learn C++
  • Embarcadero Blogs
  • BeginEnd.net
  • Python GUI
  • Firebird News
  • Torry’s Delphi Pages
Copyright DelphiFeeds.com 2021. All Rights Reserved
Embarcadero
Login Register

Login

Lost Password

Register

Lost Password