I need something like GetKeyboardState() but for mouse. I do not need to handle an event when the mouse button pressed, just need to know if the buttons are up or down at some moment. This is for toy animation program.
if (GetAsyncKeyState(VK_LBUTTON) < 0)
{
// left button is down
}
if (GetAsyncKeyState(VK_RBUTTON) < 0)
{
// right button is down
}
Related
I got way to ignore mouse event for NSWindow. Surely It makes all window rect disable for mouse event.
I want to know ignore mouse event for NSView(part of window)
How can I make only red rect to ignore mouse event?
import SwiftUI
#main
struct app1App: App {
var body: some Scene {
WindowGroup {
ContentView()
.allowsHitTesting(false)
Button("Ignore Mouse Events") {
NSApp.setActivationPolicy(.accessory)
for window in NSApplication.shared.windows {
window.isOpaque = false;
window.backgroundColor = NSColor.clear;
window.level = .floating
window.ignoresMouseEvents = true;
}
}
}
}
}
I want to make a doodling application, drawing strokes when mouse is moving, in the mousemove event handler, below code works:
line(lastX, lastY, mouseX, mouseY);
lastX = mouseX;
lastY = mouseY;
However, when I wrap the codes into an if statement like:
if (mouseIsPressed) {
...
}
Nothing, if I move the codes into draw function and set that condition, it works. Why?
Thanks in advance.
From the mouseMoved() reference:
The mouseMoved() function is called every time the mouse moves and a mouse button is not pressed.
So the mouseMoved() function won't be called if you are pressing a mouse button. If you put some code into an if block like:
if (mouseIsPressed) {
...
}
and put that block into mouseMoved(), then that code will never be executed.
It's totally ok to put that if block directly in the draw() function. You don't have to use mouseMoved() in this case :)
I have a kinetic stage and some objects that each are draggable. (See fiddle for example.)
My goal is to allow the user to always be able to middle click to pan the entire stage whether or not he middle clicks on empty space or on any shape drawn to the stage. I need to do this in a way that doesn't break the drag events of existing shapes. Meaning I need the shape dragStart and dragEnd events to be processed normally, but only on left click drag.
I'm able to detect the middle click event on the stage and call dragStop() on the shape if the drag was initiated on one, but calling dragStop() means that the dragstop event is fired and processing occurs on the shape.
// let's pretend that a mouse doesn't have more than 9 buttons
var mouseDown = [0, 0, 0, 0, 0, 0, 0, 0, 0];
var mouseDownCount = 0;
document.body.onmousedown = function(evt) {
++mouseDown[evt.button];
++mouseDownCount;
}
document.body.onmouseup = function(evt) {
--mouseDown[evt.button];
--mouseDownCount;
}
stage.on('dragstart', function(e){
if(mouseDownCount){
if(mouseDown[0]){
// Left mouse button drag
if (e.target.nodeType == 'Stage'){
// stop the drag for left click
e.target.stopDrag();
}
} else if (mouseDown[1]){
// Middle mouse button drag
if (e.target.nodeType != 'Stage'){
// stop the drag of the object for left click
e.target.listening(false);
e.target.stopDrag();
e.target.listening(true);
// start the stage drag
e.target.getStage().startDrag()
}
}
}
});
Is there a way to cancel the dragStart event after it has been fired, or in some other way tell dragEnd to not fire? I've tried setting e.target.listening(false) prior to calling dragStop, but that doesn't seem to be working.
Any ideas?
Try to use latest version from GitHub. It has functionality only with left mouse button.
I wrote this code to draw a simple rectangle in a dialog , I also added ON_WM_PAINT() to my message map. but it didnt show anything on dialog to me ! I really appreciate it if anyone could tell my mistakes in code:
void Ctest4Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = 2;
int y = 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
//I want to draw a rectangle
dc.Rectangle(10,10,50,50);
}
else
{
CDialogEx::OnPaint();
}
}
Looks like your paint code only runs when the window is iconic? Why are you doing that?
Put it in the else block, after the call to CDialogEx::OnPaint().
Your first and biggest mistake is trying to draw directly in a dialog. While it is possible to do so, it's almost always a bad idea. A dialog should usually be treated as a container for controls.
How do I change the background color of a button control created using CreateWindow?
The Windows API does not offer many options to customize the appearance of standard controls anymore.
WM_CTLCOLORBTN can be handled by the parent window of a button to control some aspects of a buttons appearance, but uxtheme buttons only use the background brush to paint the area behind the button. The appearance of the face is determined by the current theme.
WM_DRAWITEM can also be handled by the parent window, by setting the BS_OWNERDRAW style on the button. This allows the parent window to completely replace the normal buttons painting logic.
To manage the color of the controls on your dialog box, add a handler to the WM_CTLCOLOR message in your dialog class.
You will then have to add few lines like that :
HBRUSH CYourDialogClass::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_OF_YOUR_BUTTON)
{
pDC->SetBkColor (RGB(0, 0, 255)); // BLUE color for background
pDC->SetTextColor (RGB(255, 0, 0)); // RED color for text
}
return hbr;
}