理解Mac和iOS中的 Event 处理
根据现在的理解,我把event处理分为5部分,第一是,Event处理的Architecture;第二是,Event的Dispatch到first responder之前; 第三是,Event从first responder出来后;第四是,Action与Event的关系;第五是,Apple Event的处理。
1. Event Handling Architecture
下面这张图解释了在MacOSX上event处理的过程。

Before it dispatches an event to an application, the window server processes it in various ways; it time-stamps it, annotates it with the associated window and process port, and possibly performs other tasks as well. As an example, consider what happens when a user presses a key. The device driver translates the raw scan code into a virtual key code which it then passes off (along with other information about the key-press) to the window server in an event record. The window server has a translation facility that converts the virtual key code into a Unicode character.
In OS X, events are delivered as an asynchronous stream. This event stream proceeds “upward” (in an architectural sense) through the various levels of the system—the hardware to the window server to the Event Manager—until each event reaches its final destination: an application. As it passes through each subsystem, an event may change structure but it still identifies a specific user action.
在event到达Window Server之后,就等着发给各个Application去处理了,如果一个App是基于Cocoa的,它会使用Run Loop从Window Server中获取应该由本App处理的event。

从这个图可以看出,每个应用有一个Event Source与之对应。Event Source中有个Mach port用于从Window Server接收Event,然后存放在一个队列中。每个应用的Main run Loop是一个只负责从event source中取 event,如果没有event,Run Loop就会Block住,一直等到有event为止。之所以这里有个Main run loop是因为,在Cocoa应用中,每个线程都有一个run loop,主线程的run loop叫main run loop。Run loop就是一个NSRunLoop对象。Apple event一会通过Run Loop进入一个App,详细可以见Apple Event部分的描述。
2. Event Dispatch : Arriving at First Responder;
3. Event Dispatch: After First Responder;
4. Action
参考文档
1. Cocoa Event Handling Guide: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html#//apple_ref/doc/uid/10000060i-CH3-SW2
2. Event Handling Guide for iOS:https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html#//apple_ref/doc/uid/TP40009541-CH4-SW2
3. Mac vs. iOS event 比较:https://developer.apple.com/library/ios/documentation/general/conceptual/Devpedia-CocoaApp/Responder.html
理解Mac和iOS中的 Event 处理的更多相关文章
- iOS中多线程原理与runloop介绍
一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...
- IOS中的多线程和NSRunLoop概述(转载)
线程概述 有些程序是一条直线,从起点到终点,如Hello World,运行打印完,它的生命周期便结束了:有些程序是一个圆,不断循环,直到将它切断,如操作系统,一直运行直到你关机. 一个运行着的程序就 ...
- 在Quick-cocos2dx中使用云风pbc解析Protocol Buffers,支持win、mac、ios、android
本例主要介绍 如何将 pbc 集成到quick-cocos2dx框架中,让我们的cocos2dx客户端Lua拥有编解码Protocol Buffers能力. 参考: 云风pbc的用法: http:// ...
- [NodeJs系列][译]理解NodeJs中的Event Loop、Timers以及process.nextTick()
译者注: 为什么要翻译?其实在翻译这篇文章前,笔者有Google了一下中文翻译,看的不是很明白,所以才有自己翻译的打算,当然能力有限,文中或有错漏,欢迎指正. 文末会有几个小问题,大家不妨一起思考一下 ...
- iOS中web app调试(mac)
原文 iOS中web app调试(mac).md 目录 一.真机联调配置 二.mac上Safari配置及真机联调 三.iOS模拟器使用 四.在iOS模拟器中安装app 近期公司vue项目开发,目的是一 ...
- iOS中的堆(heap)和栈(stack)的理解
操作系统iOS 中应用程序使用的计算机内存不是统一分配空间,运行代码使用的空间在三个不同的内存区域,分成三个段:“text segment “,“stack segment ”,“heap segme ...
- iOS中 常用的mac终端指令汇总 技术分享
通常情况下,只有高端用户才会经常用到终端应用.这并不意味着命令行非常难学,有的时候命令行可以轻松.快速的解决问题.相信所有Mac用户都尝试过命令行,今天为大家带来9个非常实用的命令行操作.一些命令行需 ...
- IOS中调用系统的电话、短信、邮件、浏览功能
iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...
- iOS中数据库应用基础
iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...
随机推荐
- java 零碎1
1. java 程序的入口必须是 static 类型的,接口中不允许有 static , 而且接口中的方法必须是public. 2. java 回收主要针对的是堆区的回收. 3. java.exe 是 ...
- C#中的ManagementClass类
C# 提供了ManagementClass类来获取本机的一些基本信息,比如CPU的个数,CPU的频率,网卡的MAC,内存的大小,硬盘的大小等. 获取本机MAC地址: /// <summary&g ...
- Linq分页查询
//Linq分页查询 int pageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]); int ...
- js对字符串函数之charAt()
var str="012345"; 字符串下标的index从0开始: charAt(index)返回指定位置的字符如果index不在0-str.length之间,返回一个空字符串: ...
- 《Java程序设计》第五周学习总结
20145224 <Java程序设计>第五周学习总结 教材学习内容总结 第八章异常处理 8.1.1使用try.catch ·教材范例用户连续输入整数,输入0结束后显示输入数的平均值(代码如 ...
- HDFS使用0
创建上传文件的:
- C++ 如何重复利用一个内存地址块
#include<iostream> using namespace std; ; }; int main(int argv ,char * args[]) { double *p1,*p ...
- hdu---(4515)小Q系列故事——世界上最遥远的距离(模拟题)
小Q系列故事——世界上最遥远的距离 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)T ...
- C++统计一段文字中各单词出现的频率
#include <iostream> using namespace std; /* run this program using the console pauser or add y ...
- javaScript内置类Date,Math等
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> & ...