我使用的是一個鍵盤密集型應用程序。雙手放在鍵盤上。沒有手的鼠標。選擇popUpContextMenu中的第一個項目
用戶可以通過鍵盤彈出一個上下文菜單,選擇一個項目並最終點擊回車。
[NSMenu popUpContextMenu]
顯示菜單而不突出顯示任何項目。用戶將不得不一次按下箭頭鍵以突出顯示第一個項目。
我的一位朋友觀察到,每次使用此菜單時都必須按箭頭鍵,並且 建議我刪除此步驟,以便在菜單彈出時始終突出顯示第一項。
我懷疑它需要碳黑嗎?
如何以編程方式突出顯示第一項?
我使用此代碼彈出菜單。
NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined
location:location
modifierFlags:0
timestamp:0
windowNumber:[[self window] windowNumber]
context:[[self window] graphicsContext]
subtype:100
data1:0
data2:0
];
[NSMenu popUpContextMenu:menu withEvent:event forView:self];
更新:我曾嘗試popUpContextMenu後立即發送我的應用程序的arrow_down事件,但不執行事件時,菜單可見。 (該事件在菜單消失後執行)。
unichar code = NSDownArrowFunctionKey;
NSString* chars = [NSString stringWithFormat: @"%C", code];
NSEvent* event = [NSEvent keyEventWithType:NSKeyDown location:location modifierFlags:0 timestamp:0 windowNumber:[[self window] windowNumber] context:[[self window] graphicsContext] characters:chars charactersIgnoringModifiers:chars isARepeat:NO keyCode:code];
[NSApp sendEvent:event];