Checking key state under Linux and Mac

481 views
Skip to first unread message

Stephen Cheng

unread,
Dec 1, 2013, 11:48:11 AM12/1/13
to chromi...@chromium.org
Is there any platform agnostic api to check key state? I found base::win::IsShiftPressed(). What are the equivalent functions under Mac and Linux?

Thanks for help!


Stuart Morgan

unread,
Dec 1, 2013, 3:42:24 PM12/1/13
to Stephen Cheng, chromium-dev

+the list, which I dropped accidentally.

On the Mac (I don't know about other platforms) you need to check the key state *on the selection event*, not the immediate key state, for what you are trying to do to work correctly.

-Stuart

On Dec 1, 2013 10:48 AM, "Stephen Cheng" <flas...@gmail.com> wrote:
The context is like this. The user selects a menu to do some action. But I'd like to check the state of the assisted keys so that I can modify the default behavior of the action if a certain key is pressed.
 

Stephen Cheng
 
Date: 2013-12-01 11:28
Subject: Re: [chromium-dev] Checking key state under Linux and Mac

What's the context for this? Checking immediate key state is almost never what you want on Mac; instead you generally want to get the state of the event currently being processed, which gets complicated outside of the browser process.

-Stuart

On Dec 1, 2013 8:48 AM, "Stephen Cheng" <flas...@gmail.com> wrote:
Is there any platform agnostic api to check key state? I found base::win::IsShiftPressed(). What are the equivalent functions under Mac and Linux?

Thanks for help!


--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

Darin Fisher

unread,
Dec 1, 2013, 4:44:11 PM12/1/13
to Stuart Morgan, Stephen Cheng, chromium-dev
Note: IsShiftPressed() is implemented using GetKeyState, which queries the state of the keyboard as it was when the current input event was generated. Win32 provides this function as a convenience so you don't have to keep the last received MSG around somewhere for context.

(GetAsyncKeyState is the function that checks the immediate key state.)

-Darin

Stephen Cheng

unread,
Dec 1, 2013, 9:50:34 PM12/1/13
to Darin Fisher, Stuart Morgan, chromium-dev
Yes. I am familar with the win32 apis. But I have very little knowledge on linux and mac.
 

Stephen Cheng

Rachel Blum

unread,
Dec 2, 2013, 5:42:56 PM12/2/13
to flas...@gmail.com, Darin Fisher, Stuart Morgan, chromium-dev
On OSX, _if_ you are in the browser process, use
ui::EventFlagsFromNSEvent([NSApp currentEvent]);

If you're using Views and you have a current event:
event.IsShiftDown()

Gtk:
GdkEvent* event = gtk_get_current_event();
int event_flags = event_utils::EventFlagsFromGdkState(event->button.state);

But whatever you do, if you want to write linux/mac specific code, I'd strongly suggest acquiring the knowledge necessary. You'll be able to get occasional snippets from the mailing list, but when they (inevitably) don't work quite the way you intend, people won't be able to help since they don't know your code.

 - rachel

Stephen Cheng

unread,
Dec 2, 2013, 5:53:13 PM12/2/13
to Rachel Blum, Darin Fisher, Stuart Morgan, chromium-dev
Thank a lot for the tip. My original intention was to check if there are already some standard platform agnostic api to do this. I'd like to avoid writing platform-specific code unless I have no other choices.
 

Stephen Cheng
Reply all
Reply to author
Forward
0 new messages