提交 83a65790 编写于 作者: A anashaty

8046495: KeyEvent can not be accepted in quick mouse clicking

Reviewed-by: art, pchelko
上级 55797926
...@@ -2141,19 +2141,7 @@ namespace TimeHelper { ...@@ -2141,19 +2141,7 @@ namespace TimeHelper {
} }
jlong getMessageTimeUTC() { jlong getMessageTimeUTC() {
return windowsToUTC(getMessageTimeWindows()); return ::JVM_CurrentTimeMillis(NULL, 0);
}
// If calling order of GetTickCount and JVM_CurrentTimeMillis
// is swapped, it would sometimes give different result.
// Anyway, we would not always have determinism
// and sortedness of time conversion here (due to Windows's
// timers peculiarities). Having some euristic algorithm might
// help here.
jlong windowsToUTC(DWORD windowsTime) {
jlong offset = ::GetTickCount() - windowsTime;
jlong jvm_time = ::JVM_CurrentTimeMillis(NULL, 0);
return jvm_time - offset;
} }
} //TimeHelper } //TimeHelper
...@@ -3578,7 +3566,7 @@ MsgRouting AwtComponent::WmKeyDown(UINT wkey, UINT repCnt, ...@@ -3578,7 +3566,7 @@ MsgRouting AwtComponent::WmKeyDown(UINT wkey, UINT repCnt,
SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_PRESSED, SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_PRESSED,
TimeHelper::windowsToUTC(msg.time), jkey, character, TimeHelper::getMessageTimeUTC(), jkey, character,
modifiers, keyLocation, (jlong)wkey, &msg); modifiers, keyLocation, (jlong)wkey, &msg);
// bugid 4724007: Windows does not create a WM_CHAR for the Del key // bugid 4724007: Windows does not create a WM_CHAR for the Del key
...@@ -3588,7 +3576,7 @@ MsgRouting AwtComponent::WmKeyDown(UINT wkey, UINT repCnt, ...@@ -3588,7 +3576,7 @@ MsgRouting AwtComponent::WmKeyDown(UINT wkey, UINT repCnt,
// for Java - we don't want Windows trying to process it). // for Java - we don't want Windows trying to process it).
if (jkey == java_awt_event_KeyEvent_VK_DELETE) { if (jkey == java_awt_event_KeyEvent_VK_DELETE) {
SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_TYPED, SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_TYPED,
TimeHelper::windowsToUTC(msg.time), TimeHelper::getMessageTimeUTC(),
java_awt_event_KeyEvent_VK_UNDEFINED, java_awt_event_KeyEvent_VK_UNDEFINED,
character, modifiers, character, modifiers,
java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN, (jlong)0); java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN, (jlong)0);
...@@ -3620,7 +3608,7 @@ MsgRouting AwtComponent::WmKeyUp(UINT wkey, UINT repCnt, ...@@ -3620,7 +3608,7 @@ MsgRouting AwtComponent::WmKeyUp(UINT wkey, UINT repCnt,
UpdateDynPrimaryKeymap(wkey, jkey, keyLocation, modifiers); UpdateDynPrimaryKeymap(wkey, jkey, keyLocation, modifiers);
SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_RELEASED, SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_RELEASED,
TimeHelper::windowsToUTC(msg.time), jkey, character, TimeHelper::getMessageTimeUTC(), jkey, character,
modifiers, keyLocation, (jlong)wkey, &msg); modifiers, keyLocation, (jlong)wkey, &msg);
return mrConsume; return mrConsume;
} }
...@@ -3665,7 +3653,7 @@ MsgRouting AwtComponent::WmIMEChar(UINT character, UINT repCnt, UINT flags, BOOL ...@@ -3665,7 +3653,7 @@ MsgRouting AwtComponent::WmIMEChar(UINT character, UINT repCnt, UINT flags, BOOL
jint modifiers = GetJavaModifiers(); jint modifiers = GetJavaModifiers();
SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_TYPED, SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_TYPED,
TimeHelper::windowsToUTC(msg.time), TimeHelper::getMessageTimeUTC(),
java_awt_event_KeyEvent_VK_UNDEFINED, java_awt_event_KeyEvent_VK_UNDEFINED,
unicodeChar, modifiers, unicodeChar, modifiers,
java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN, (jlong)0, java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN, (jlong)0,
...@@ -3734,7 +3722,7 @@ MsgRouting AwtComponent::WmChar(UINT character, UINT repCnt, UINT flags, ...@@ -3734,7 +3722,7 @@ MsgRouting AwtComponent::WmChar(UINT character, UINT repCnt, UINT flags,
InitMessage(&msg, message, character, InitMessage(&msg, message, character,
MAKELPARAM(repCnt, flags)); MAKELPARAM(repCnt, flags));
SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_TYPED, SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_TYPED,
TimeHelper::windowsToUTC(msg.time), TimeHelper::getMessageTimeUTC(),
java_awt_event_KeyEvent_VK_UNDEFINED, java_awt_event_KeyEvent_VK_UNDEFINED,
unicodeChar, modifiers, unicodeChar, modifiers,
java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN, (jlong)0, java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN, (jlong)0,
......
...@@ -325,7 +325,7 @@ static int clickCount = 0; ...@@ -325,7 +325,7 @@ static int clickCount = 0;
MsgRouting AwtTrayIcon::WmMouseDown(UINT flags, int x, int y, int button) MsgRouting AwtTrayIcon::WmMouseDown(UINT flags, int x, int y, int button)
{ {
jlong now = TimeHelper::windowsToUTC(::GetTickCount()); jlong now = TimeHelper::getMessageTimeUTC();
jint javaModif = AwtComponent::GetJavaModifiers(); jint javaModif = AwtComponent::GetJavaModifiers();
if (lastClickTrIc == this && if (lastClickTrIc == this &&
...@@ -361,14 +361,14 @@ MsgRouting AwtTrayIcon::WmMouseUp(UINT flags, int x, int y, int button) ...@@ -361,14 +361,14 @@ MsgRouting AwtTrayIcon::WmMouseUp(UINT flags, int x, int y, int button)
MSG msg; MSG msg;
AwtComponent::InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y); AwtComponent::InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y);
SendMouseEvent(java_awt_event_MouseEvent_MOUSE_RELEASED, TimeHelper::windowsToUTC(::GetTickCount()), SendMouseEvent(java_awt_event_MouseEvent_MOUSE_RELEASED, TimeHelper::getMessageTimeUTC(),
x, y, AwtComponent::GetJavaModifiers(), clickCount, x, y, AwtComponent::GetJavaModifiers(), clickCount,
(AwtComponent::GetButton(button) == java_awt_event_MouseEvent_BUTTON3 ? (AwtComponent::GetButton(button) == java_awt_event_MouseEvent_BUTTON3 ?
TRUE : FALSE), AwtComponent::GetButton(button), &msg); TRUE : FALSE), AwtComponent::GetButton(button), &msg);
if ((m_mouseButtonClickAllowed & AwtComponent::GetButtonMK(button)) != 0) { // No up-button in the drag-state if ((m_mouseButtonClickAllowed & AwtComponent::GetButtonMK(button)) != 0) { // No up-button in the drag-state
SendMouseEvent(java_awt_event_MouseEvent_MOUSE_CLICKED, SendMouseEvent(java_awt_event_MouseEvent_MOUSE_CLICKED,
TimeHelper::windowsToUTC(::GetTickCount()), x, y, AwtComponent::GetJavaModifiers(), TimeHelper::getMessageTimeUTC(), x, y, AwtComponent::GetJavaModifiers(),
clickCount, JNI_FALSE, AwtComponent::GetButton(button)); clickCount, JNI_FALSE, AwtComponent::GetButton(button));
} }
m_mouseButtonClickAllowed &= ~AwtComponent::GetButtonMK(button); // Exclude the up-button from the drag-state m_mouseButtonClickAllowed &= ~AwtComponent::GetButtonMK(button); // Exclude the up-button from the drag-state
...@@ -395,7 +395,7 @@ MsgRouting AwtTrayIcon::WmMouseMove(UINT flags, int x, int y) ...@@ -395,7 +395,7 @@ MsgRouting AwtTrayIcon::WmMouseMove(UINT flags, int x, int y)
if ((flags & ALL_MK_BUTTONS) != 0) { if ((flags & ALL_MK_BUTTONS) != 0) {
m_mouseButtonClickAllowed = 0; m_mouseButtonClickAllowed = 0;
} else { } else {
SendMouseEvent(java_awt_event_MouseEvent_MOUSE_MOVED, TimeHelper::windowsToUTC(::GetTickCount()), x, y, SendMouseEvent(java_awt_event_MouseEvent_MOUSE_MOVED, TimeHelper::getMessageTimeUTC(), x, y,
AwtComponent::GetJavaModifiers(), 0, JNI_FALSE, AwtComponent::GetJavaModifiers(), 0, JNI_FALSE,
java_awt_event_MouseEvent_NOBUTTON, &msg); java_awt_event_MouseEvent_NOBUTTON, &msg);
} }
...@@ -408,7 +408,7 @@ MsgRouting AwtTrayIcon::WmBalloonUserClick(UINT flags, int x, int y) ...@@ -408,7 +408,7 @@ MsgRouting AwtTrayIcon::WmBalloonUserClick(UINT flags, int x, int y)
if (AwtComponent::GetJavaModifiers() & java_awt_event_InputEvent_BUTTON1_DOWN_MASK) { if (AwtComponent::GetJavaModifiers() & java_awt_event_InputEvent_BUTTON1_DOWN_MASK) {
MSG msg; MSG msg;
AwtComponent::InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y); AwtComponent::InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y);
SendActionEvent(java_awt_event_ActionEvent_ACTION_PERFORMED, TimeHelper::windowsToUTC(::GetTickCount()), SendActionEvent(java_awt_event_ActionEvent_ACTION_PERFORMED, TimeHelper::getMessageTimeUTC(),
AwtComponent::GetJavaModifiers(), &msg); AwtComponent::GetJavaModifiers(), &msg);
} }
return mrConsume; return mrConsume;
...@@ -417,14 +417,14 @@ MsgRouting AwtTrayIcon::WmBalloonUserClick(UINT flags, int x, int y) ...@@ -417,14 +417,14 @@ MsgRouting AwtTrayIcon::WmBalloonUserClick(UINT flags, int x, int y)
MsgRouting AwtTrayIcon::WmKeySelect(UINT flags, int x, int y) MsgRouting AwtTrayIcon::WmKeySelect(UINT flags, int x, int y)
{ {
static jlong lastKeySelectTime = 0; static jlong lastKeySelectTime = 0;
jlong now = TimeHelper::windowsToUTC(::GetTickCount()); jlong now = TimeHelper::getMessageTimeUTC();
// If a user selects a notify icon with the ENTER key, // If a user selects a notify icon with the ENTER key,
// Shell 5.0 sends double NIN_KEYSELECT notification. // Shell 5.0 sends double NIN_KEYSELECT notification.
if (lastKeySelectTime != now) { if (lastKeySelectTime != now) {
MSG msg; MSG msg;
AwtComponent::InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y); AwtComponent::InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y);
SendActionEvent(java_awt_event_ActionEvent_ACTION_PERFORMED, TimeHelper::windowsToUTC(::GetTickCount()), SendActionEvent(java_awt_event_ActionEvent_ACTION_PERFORMED, TimeHelper::getMessageTimeUTC(),
AwtComponent::GetJavaModifiers(), &msg); AwtComponent::GetJavaModifiers(), &msg);
} }
lastKeySelectTime = now; lastKeySelectTime = now;
...@@ -441,7 +441,7 @@ MsgRouting AwtTrayIcon::WmSelect(UINT flags, int x, int y) ...@@ -441,7 +441,7 @@ MsgRouting AwtTrayIcon::WmSelect(UINT flags, int x, int y)
if (clickCount == 2) { if (clickCount == 2) {
MSG msg; MSG msg;
AwtComponent::InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y); AwtComponent::InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y);
SendActionEvent(java_awt_event_ActionEvent_ACTION_PERFORMED, TimeHelper::windowsToUTC(::GetTickCount()), SendActionEvent(java_awt_event_ActionEvent_ACTION_PERFORMED, TimeHelper::getMessageTimeUTC(),
AwtComponent::GetJavaModifiers(), &msg); AwtComponent::GetJavaModifiers(), &msg);
} }
return mrConsume; return mrConsume;
......
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import sun.awt.SunToolkit;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
/*
* @test
* @bug 8046495
* @summary Verifies that mouse/key events has always increasing 'when' timestamps
* @author Anton Nashatyrev
* @run main EventWhenTest
*/
public class EventWhenTest {
private static volatile int eventsCount = 0;
private static volatile boolean failed = false;
static {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
long lastWhen = 0;
@Override
public void eventDispatched(AWTEvent event) {
long curWhen;
if (event instanceof KeyEvent) {
curWhen = ((KeyEvent) event).getWhen();
} else if (event instanceof MouseEvent) {
curWhen = ((MouseEvent) event).getWhen();
} else {
return;
}
eventsCount++;
if (curWhen < lastWhen) {
System.err.println("FAILED: " + curWhen + " < " + lastWhen +
" for " + event);
failed = true;
} else {
lastWhen = curWhen;
}
}
}, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
}
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Frame frame = new Frame();
try {
Button b = new Button("Button");
frame.setBounds(300, 300, 300, 300);
frame.add(b);
frame.setVisible(true);
toolkit.realSync();
Robot robot = new Robot();
robot.mouseMove((int)frame.getLocationOnScreen().getX() + 150,
(int)frame.getLocationOnScreen().getY() + 150);
eventsCount = 0;
System.out.println("Clicking mouse...");
for (int i = 0; i < 300 && !failed; i++) {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(10);
b.setLabel("Click: " + i);
}
if (eventsCount == 0) {
throw new RuntimeException("No events were received");
}
if (failed) {
throw new RuntimeException("Test failed.");
}
System.out.println("Clicking mouse done: " + eventsCount + " events.");
b.requestFocusInWindow();
toolkit.realSync();
eventsCount = 0;
System.out.println("Typing a key...");
for (int i = 0; i < 300 && !failed; i++) {
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
Thread.sleep(10);
b.setLabel("Type: " + i);
}
System.out.println("Key typing done: " + eventsCount + " events.");
if (eventsCount == 0) {
throw new RuntimeException("No events were received");
}
if (failed) {
throw new RuntimeException("Test failed.");
}
System.out.println("Success!");
} finally {
frame.dispose();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册