Example #1
0
 public static void KeyUp(NSKey _key)
 {
     using (var eventSource = new CGEventSource(CGEventSourceStateID.HidSystem))
     {
         var key = (ushort)_key;
         using (var keyEvent = new CGEvent(eventSource, key, false))
         {
             CGEvent.Post(keyEvent, CGEventTapLocation.HID);
         }
     }
 }
Example #2
0
        public MouseMonitor()
        {
            tapPort = CGEvent.CreateTap(
                CGEventTapLocation.Session,
                CGEventTapPlacement.TailAppend,
                CGEventTapOptions.ListenOnly,
                CGEventMask.LeftMouseUp | CGEventMask.MouseMoved,
                eventTapCallback = new CGEvent.CGEventTapCallback(HandleEventTap),
                IntPtr.Zero);

            runLoopSource = tapPort.CreateRunLoopSource();
            CFRunLoop.Current.AddSource(runLoopSource, CFRunLoop.ModeDefault);
        }
Example #3
0
        static IntPtr tapEventCallback(IntPtr proxy, CGEventType type, IntPtr evtHandle, IntPtr info)
        {
            using (new NSAutoreleasePool()) {
                var evt = new CGEvent(evtHandle);
                if (!SPMediaKeyTap.Shared.ShouldInterceptMediaKeyEvents)
                {
                    return(evtHandle);
                }
                switch (type)
                {
                //kCGEventTapDisabledByTimeout
                case  (CGEventType)kCGEventTapDisabledByTimeout:
                    CGEvent.TapEnable(SPMediaKeyTap.Shared.eventPort);
                    return(evtHandle);

                case (CGEventType)kCGEventTapDisabledByUser:
                    return(evtHandle);
                }

                NSEvent nsEvent = null;
                try{
                    nsEvent = NSEvent.EventWithCGEvent(evtHandle);
                }
                catch (Exception ex) {
                    return(evtHandle);
                }

                if (nsEvent.Subtype != SPSystemDefinedEventMediaKeys)
                {
                    return(evtHandle);
                }
                long keyCode = ((nsEvent.Data1 & 0xFFFF0000) >> 16);

                var keyFlags = (nsEvent.Data1 & 0x0000FFFF);

                var keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA;
                if (keyCode == 10 && keyFlags == 6972)
                {
                    switch (nsEvent.Data2)
                    {
                    case 786608:                     // Play / Pause on OS < 10.10 Yosemite
                    case 786637:                     // Play / Pause on OS >= 10.10 Yosemite
                        Console.WriteLine(@"Play/Pause bluetooth keypress detected...sending corresponding media key event");
                        SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Play);
                        break;

                    case 786611:                     // Next
                        Console.WriteLine(@"Next bluetooth keypress detected...sending corresponding media key event");
                        SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Next);
                        break;

                    case 786612:                     // Previous
                        Console.WriteLine(@"Previous bluetooth keypress detected...sending corresponding media key event");
                        SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Previous);
                        break;

                    case 786613:                     // Fast-forward
                        Console.WriteLine(@"Fast-forward bluetooth keypress detected...sending corresponding media key event");
                        SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Fast);
                        break;

                    case 786614:                     // Rewind
                        Console.WriteLine(@"Rewind bluetooth keypress detected...sending corresponding media key event");
                        SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Rewind);
                        break;

                    default:
                        // TODO make this popup a message in the UI (with a link to submit the issue and a "don't show this message again" checkbox)
                        LogManager.Shared.Log($"Unknown bluetooth key received: keyCode:{keyCode} keyFlags:{keyFlags} keyState:{keyState} {nsEvent.Data2}");
                        return(evtHandle);
                    }
                }
                else
                {
                    if (keyCode != NX_KEYTYPE_PLAY && keyCode != NX_KEYTYPE_FAST && keyCode != NX_KEYTYPE_REWIND && keyCode != NX_KEYTYPE_PREVIOUS && keyCode != NX_KEYTYPE_NEXT)
                    {
                        return(evtHandle);
                    }
                    //These always come in pairs
                    if (lastKeyCode != keyCode)
                    {
                        SPMediaKeyTap.Shared.BeginInvokeOnMainThread(() => {
                            SPMediaKeyTap.Shared.RecievedEvent((MediaKey)keyCode);
                        });
                        lastKeyCode = keyCode;
                    }
                    else
                    {
                        lastKeyCode = 0;
                    }
                }
                evt.Dispose();
                return(IntPtr.Zero);
            }
        }
Example #4
0
 public extern static CGEvent CGEventPost(ref CGEvent source, CGEventType mouseType, CGPoint mouseCursorPosition, CGMouseButton mouseButton);
Example #5
0
 public extern static CGPoint CGEventGetLocation(ref CGEvent eventRef);
        public override void DidFinishLaunching(NSNotification notification)
        {
            // Enumerate the MIDI devices available

            var access = MidiAccessManager.Default;

            foreach (var i in access.Inputs)
            {
                Console.WriteLine(i.Id + " : " + i.Name);
            }
            if (!access.Inputs.Any())
            {
                Console.WriteLine("No input device found.");
                return;
            }

            var noteToKeyboardMap = new Dictionary <byte, KeyboardEvent>();

            noteToKeyboardMap.Add(60, new KeyboardEvent((ushort)NSKey.F10, CGEventFlags.Control | CGEventFlags.Shift | CGEventFlags.Command));
            noteToKeyboardMap.Add(61, new KeyboardEvent((ushort)NSKey.F11, CGEventFlags.Control | CGEventFlags.Shift | CGEventFlags.Command));
            noteToKeyboardMap.Add(62, new KeyboardEvent((ushort)NSKey.F12, CGEventFlags.Control | CGEventFlags.Shift | CGEventFlags.Command));

            var iport = access.Inputs.FirstOrDefault(i => i.Id == "Arduino Leonardo") ?? access.Inputs.Last();
            var input = access.OpenInputAsync(iport.Id).Result;

            Console.WriteLine("Using " + iport.Id);
            input.MessageReceived += (obj, e) => {
                Console.WriteLine($"{e.Timestamp} {e.Start} {e.Length} {e.Data[0].ToString("X")}");

                bool noteEvent = false;
                bool noteOn    = false;
                byte channel   = 0;
                byte midiKey   = 0;
                byte velocity  = 0;

                // Decode the MIDI packet for note information
                byte startByte = e.Data[0];
                if ((startByte & 0x90) == 0x90)
                {
                    // We have a Note On event
                    noteEvent = true;
                    noteOn    = true;

                    // Decode the channel
                    channel = (byte)(startByte & 0x0F);

                    // Decode the key value
                    midiKey = (byte)(e.Data[1] & 0x7F);

                    // Decode the velocity
                    velocity = (byte)(e.Data[2] & 0x7F);
                }
                else if ((startByte & 0x80) == 0x80)
                {
                    // We have a Note Off event
                    noteEvent = true;
                    noteOn    = false;

                    // Decode the channel
                    channel = (byte)(startByte & 0x0F);

                    // Decode the key value
                    midiKey = (byte)(e.Data[1] & 0x7F);

                    // Decode the velocity
                    velocity = (byte)(e.Data[2] & 0x7F);
                }

                if (noteEvent == true)
                {
                    // Calculate keyboard event to send based on MIDI key
                    var          key  = noteToKeyboardMap[midiKey].keyValue;
                    CGEventFlags mods = noteToKeyboardMap[midiKey].modifierKeys;


                    InvokeOnMainThread(() => // Need to run the keyboard interaction code on the main thread
                    {
                        using (var eventSource = new CGEventSource(CGEventSourceStateID.CombinedSession))
                        {
                            if (noteOn)
                            {
                                var keyDownEvent   = new CGEvent(eventSource, key, true);
                                keyDownEvent.Flags = mods;
                                CGEvent.Post(keyDownEvent, CGEventTapLocation.AnnotatedSession);
                            }
                            else
                            {
                                var keyUpEvent   = new CGEvent(eventSource, key, false);
                                keyUpEvent.Flags = mods;
                                CGEvent.Post(keyUpEvent, CGEventTapLocation.AnnotatedSession);
                            }
                        };
                    });
                }
            };
        }
Example #7
0
        public override CGObject ReadObject(BinaryReader reader, int objectId, MapPosition objectPosition)
        {
            CGEvent eventObject = new CGEvent();

            ReadMessageAndGuards(reader, eventObject);

            var gainedExp  = reader.ReadUInt32();
            var manaDiff   = reader.ReadUInt32();
            var moraleDiff = reader.ReadByte();
            var luckDiff   = reader.ReadByte();

            ResourceSet resources = ReadResources(reader);

            for (int x = 0; x < 4; x++)
            {
                var primSkill = (EPrimarySkill)reader.ReadByte();
            }

            int gainedAbilities = reader.ReadByte();

            for (int i = 0; i < gainedAbilities; i++)
            {
                ESecondarySkill      skill = (ESecondarySkill)reader.ReadByte();
                ESecondarySkillLevel level = (ESecondarySkillLevel)reader.ReadByte();

                eventObject.Abilities.Add(new AbilitySkill(skill, level));
            }

            int gainedArtifacts = reader.ReadByte();

            for (int i = 0; i < gainedArtifacts; i++)
            {
                if (MapHeader.Version == EMapFormat.ROE)
                {
                    var artId = (EArtifactId)reader.ReadByte();
                }
                else
                {
                    var artId = (EArtifactId)reader.ReadUInt16();
                }
            }

            int gainedSpells = reader.ReadByte();

            for (int i = 0; i < gainedSpells; i++)
            {
                var spellId = (ESpellId)reader.ReadByte();
            }

            int gainedCreatures = reader.ReadByte();
            var creatureSet     = ReadCreatureSet(reader, gainedCreatures);

            reader.Skip(8);

            var availableForPlayer = reader.ReadByte();
            var computerActivate   = reader.ReadByte();
            var removeAfterVisit   = reader.ReadByte();
            var humanActivate      = true;

            reader.Skip(4);

            return(eventObject);
        }