Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6063407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:09:44+00:00 2026-05-23T09:09:44+00:00

I am programatically generating mouse clicks when a user clicks a certain keyboard key

  • 0

I am programatically generating mouse clicks when a user clicks a certain keyboard key (CapsLock).
So I do a left mouse down when CapsLock is switched on, then a left mouse up when CapsLock is switched off.

This behaves correctly in that if I for example place the mouse over a window title bar, click CapsLock, then move the mouse, then click CapsLock, the window correctly moves. i.e. I correctly ‘drag’ the window as if I had held the left mouse button down whilst moving the mouse.

However there is one problem – the window does not move whilst I am moving the mouse, it only moves to the final position after I have clicked CapsLock a second time. i.e. after I have ‘released’ the mouse button.

What do I need to do to ensure the screen is refreshed during the mouse move?

Interestingly, I also hooked to

[NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDraggedMask

and found that my NSLog statement only output after I released the left mouse button (the real left mouse button)

Mouse click code is below, I can post all the code if necessary, there isn’t much of it..

// simulate mouse down

// get current mouse pos
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);

CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef theEvent = CGEventCreateMouseEvent(source, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);
CGEventSetType(theEvent, kCGEventLeftMouseDown);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);


// simulate mouse up

// get current mouse pos
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);

CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef theEvent = CGEventCreateMouseEvent(source, kCGEventLeftMouseUp, point, kCGMouseButtonLeft);
CGEventSetType(theEvent, kCGEventLeftMouseUp);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:09:45+00:00Added an answer on May 23, 2026 at 9:09 am

    If you want to be able to drag windows, the problem is that you also need to post a LeftMouseDragged event.

    Simply call beginEventMonitoring to start listening for caps lock key events and mouse move events. The event handlers will simulate a left mouse press and movement just as you wanted. Here is a link to my blog where you can download a full working example for Xcode 4: http://www.jakepetroules.com/2011/06/25/simulating-mouse-events-in-cocoa

    The example is in the public domain, do whatever you like with it. 🙂

    According to Apple (NSEvent documentation), “Enable access for assistive devices” needs to be checked in System Preferences > Universal Access for this to work, but I have it unchecked and it wasn’t an issue. Just a heads up.

    Please let me know if you have any further issues and I will try my best to help.

    // Begin listening for caps lock key presses and mouse movements
    - (void)beginEventMonitoring
    {
        // Determines whether the caps lock key was initially down before we started listening for events
        wasCapsLockDown = CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, kVK_CapsLock);
    
        capsLockEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSFlagsChangedMask) handler: ^(NSEvent *event)
        {
            // Determines whether the caps lock key was pressed and posts a mouse down or mouse up event depending on its state
            bool isCapsLockDown = [event modifierFlags] & NSAlphaShiftKeyMask;
            if (isCapsLockDown && !wasCapsLockDown)
            {
                [self simulateMouseEvent: kCGEventLeftMouseDown];
                wasCapsLockDown = true;
            }
            else if (wasCapsLockDown)
            {
                [self simulateMouseEvent: kCGEventLeftMouseUp];
                wasCapsLockDown = false;
            }
        }];
    
        mouseMovementEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSMouseMovedMask) handler:^(NSEvent *event)
        {
            [self simulateMouseEvent: kCGEventLeftMouseDragged];
        }];
    }
    
    // Cease listening for caps lock key presses and mouse movements
    - (void)endEventMonitoring
    {
        if (capsLockEventMonitor)
        {
            [NSEvent removeMonitor: capsLockEventMonitor];
            capsLockEventMonitor = nil;
        }
    
        if (mouseMovementEventMonitor)
        {
            [NSEvent removeMonitor: mouseMovementEventMonitor];
            mouseMovementEventMonitor = nil;
        }
    }
    
    -(void)simulateMouseEvent:(CGEventType)eventType
    {
        // Get the current mouse position
        CGEventRef ourEvent = CGEventCreate(NULL);
        CGPoint mouseLocation = CGEventGetLocation(ourEvent);
    
        // Create and post the event
        CGEventRef event = CGEventCreateMouseEvent(CGEventSourceCreate(kCGEventSourceStateHIDSystemState), eventType, mouseLocation, kCGMouseButtonLeft);
        CGEventPost(kCGHIDEventTap, event);
        CFRelease(event);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's a good method of programatically generating etag for web pages, and is this
Is it taboo to programatically create system restore points? I would be doing this
How do I do the following via C# (as I'm generating ListViews programatically): <ListView>
When I'm generating a text file programatically, should I insert the ASCII EOF marker
a minor question but it's driving me nuts. I'm programatically generating about 50 DDLs
I want to programatically create an NSTextView. How can I determine the correct frame
How can I programatically cause a control's tooltip to show in a Winforms app
We need to programatically burn files to CD in a C\C++ Windows XP/Vista application
I know I can programatically make the taskbar item for a particular window start
I need to programatically determine whether .NET 3.5 is installed. I thought it would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.