# Monitor mouse and key board events

**URL:** https://talk.remobjects.com/t/monitor-mouse-and-key-board-events/27277
**Category:** Fire
**Tags:** visual-studio, net
**Created:** [September 23, 2022, 3:40pm UTC](https://talk.remobjects.com/t/monitor-mouse-and-key-board-events/27277 "2022-09-23T15:40:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![muhammad.shahbaz](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@muhammad.shahbaz](https://talk.remobjects.com/u/muhammad.shahbaz)
#### Post date: [September 23, 2022, 3:40pm UTC](https://talk.remobjects.com/t/monitor-mouse-and-key-board-events/27277/1 "2022-09-23T15:40:39Z")

</div>

I am trying to track mouse events in RemObject c#, for OSX Application. For this I tried the solution below but it does not work. Can you tell me how is it work

```auto
public override void windowDidLoad()
		{
			base.windowDidLoad();
			
			NSEvent.addGlobalMonitorForEventsMatchingMask(NSEvent.EventTypeMask.KeyDown) handler(eventMonitorHandler);
		}

		private void eventMonitorHandler(NSEvent e)
		{			
			Swift.print(e.type);
		}

```

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [September 23, 2022, 4:08pm UTC](https://talk.remobjects.com/t/monitor-mouse-and-key-board-events/27277/2 "2022-09-23T16:08:45Z")

</div>

Hmm, this looks correct, but i can’t get it to work, either

however if I change the event type to Any, I do get MouseMoved (and _only_ MouseMoved) events… this seems to be something that’s broken on OS level, not specific to Elements, as I see many questions about this on Google, and no solutions.

```auto
            NSEvent.addGlobalMonitorForEventsMatchingMask(NSEventMask.Any) handler(( @event) => {
                Log($"pressed! {@event}");
            });

```

---

<div class="post-metadata">

### Author: ![muhammad.shahbaz](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@muhammad.shahbaz](https://talk.remobjects.com/u/muhammad.shahbaz)
#### Post date: [September 29, 2022, 3:05pm UTC](https://talk.remobjects.com/t/monitor-mouse-and-key-board-events/27277/3 "2022-09-29T15:05:14Z")

</div>

Hi, as you mentioned in your reply that might be this OS level issue therefore I have no way but to test it in swift. I have written a small program for macos which useses the NSEvent.addGlobalMonitorForEvents and I was able to capture all the events.

So, my question is that why it is not working in Elements.

[EventTest.zip](https://talk.remobjects.com/uploads/short-url/e1JxpdwRvY0pLVeptFsXSxbPZPQ.zip) (37.9 KB)

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [September 29, 2022, 3:19pm UTC](https://talk.remobjects.com/t/monitor-mouse-and-key-board-events/27277/4 "2022-09-29T15:19:41Z")

</div>

Strange. I coped your exact code over to Elements (attached), and I see only mouse events, yet I can confirm in the Xcode version I see keyboard events too…

[MacApp4 1.bugreport.zip](https://talk.remobjects.com/uploads/short-url/nodbmhzu0BGkf6r3QmoTNvzbBbS.zip) (512.0 KB)

```auto
~> Process MacApp4 started, took 0.155.
2022-09-29 11:13:38.971889-0400 MacApp4[95227:9250855] global event monitor: 1
mouse event raised
2022-09-29 11:13:39.098117-0400 MacApp4[95227:9250855] global event monitor: 2
mouse event raised
2022-09-29 11:13:39.106421-0400 MacApp4[95227:9250855] global event monitor: 3
mouse event raised
2022-09-29 11:13:39.114607-0400 MacApp4[95227:9250855] global event monitor: 4
mouse event raised

```

if I print out the value of mask, its 5164 (0x142C) in both versions. I really don’t get what the difference could be, sorry :(.

---

<div class="post-metadata">

### Author: ![muhammad.shahbaz](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@muhammad.shahbaz](https://talk.remobjects.com/u/muhammad.shahbaz)
#### Post date: [October 4, 2022, 9:35am UTC](https://talk.remobjects.com/t/monitor-mouse-and-key-board-events/27277/5 "2022-10-04T09:35:32Z")

</div>

Hello,  
Does this mean that we can not use/consume MacOS keyboard events from Elements at all?

One more question: Can this be the issue related to MacOS security?

Best Regards,  
Mansoor

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 4, 2022, 11:36am UTC](https://talk.remobjects.com/t/monitor-mouse-and-key-board-events/27277/6 "2022-10-04T11:36:34Z")

</div>

> [@muhammad.shahbaz](#):
>
> One more question: Can this be the issue related to MacOS security?

It could be, but I don’t see how/why, if it works in Xcode. Let me double-check if the Xcode project had any entitlements or anything else special set, by default (it shouldn’t).

Oddly, the Xcode project is set to run sandboxed (which should be, if anything, _more_ restricted). But worth trying the same in the Elements one

```auto
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.files.user-selected.read-only</key>
    <true/>

```

Nothing else stands out; the project is “signed to run locally” only; same as Elements does (required on arm Macs to run _anything_).

…

Oddly, now (not sure if that’s only because I added the entitlements, I do get “keyboard flags events”. but still no “key down” ones :(. I also get others with type “1”, “4” and “22” (left mouse down, right mouse down, and scrollwheel)

…

Nope, not related to the entitlements at all.

This is _really_ baffling.
