Can you turn peripherals off in WINAPI? [duplicate] - winapi

This question already has an answer here:
Programmatically disable Keyboard & Mouse
(1 answer)
Closed 4 years ago.
Is there any function in WINAPI that disables or turns off peripherals? For example
#include <Windows.h>
int main()
{
TurnMouseOff(); // what to use here
Sleep(1000);
TurnMouseOn(); // what to use here
}
or any function like MouseMovement(true) for enabled / MouseMovement(false) for disabled?

I believe it is possible to simulate a hardware unplug event (might not be possible for PS/2 devices). If you want to go down this route you would have to look at the tests performed by WLK/WHQL certification. You could try IOCTL_INTERNAL_USB_CYCLE_PORT but using SetupAPI to disable the driver might be a better approach.
If you only care about the mouse and keyboard you can create low level hooks and just eat all the input messages.

Related

What has changed? Wake Windows and turn on monitor from Windows API

I have an old C program for displaying caller ID called YAC. Fortunately, the author Jensen Harris provided the source.
15 years ago, I modified the source to turn on the monitor if the computer was awake but the monitor was off. The code below worked well, turning on the monitor and making the caller ID message visible on the screen.
// TG - add a call to turn on the monitor if it is sleeping.....
SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
Recently the behavior has changed (presumably a Windows update changed something)
Now when a Caller ID message should be displayed, the monitor turns on (as evidenced by the LED), but the screen remains black. The monitor remains in the black-screen condition for a few seconds, then turns off again.
What additional or different call is now required to cause Windows to activate the display and show the desktop? Possibly this could be forced by sending a mouse move, but is there a better way?
EDIT:
I have implemented the following additional code to press and release ESC. I was unable to find a good example of a relative mouse move of 1 pixel, so I used an example for keyboard. I will test and see if it is effective.
INPUT inputs[2];
UINT uSent;
// reference: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = VK_ESCAPE;
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wVk = VK_ESCAPE;
inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;
uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
EDIT2 - I can confirm this approach does work to cause the monitor to display video, but of course has the potential for side-effects as any keyboard or mouse action would. I would still be interested in learning of a pure API function that works to fully wake the system like SC_MONITORPOWER used to.

Apple Mac disable screen dimming and/or lock

Is there a way to programatically (e.g. from Python code) to prevent my Mac from dimming and subsequently locking the screen? Of course, after my application is done, I would like to enable normal operation again. I know about caffiniate, but that applies to the whole application...that is not what I want. At some point in my code I want to disable dimming and then at some other point I want to enable it again.. Any tips, hints, suggestions?
You can try IOKit. Use IOPMAssertionDeclareUserActivity will wake the screen if necessary and keep it awake until the user's display sleep Energy Saver settings:
IOReturn ret;
CGError err;
IOPMAssertionID assertId;
ret = IOPMAssertionDeclareUserActivity(CFSTR("Stay awake!"), kIOPMUserActiveLocal, &assertId);
if (ret == kIOReturnSuccess)
{
// The screen is on
}
However, from the documentation for that method:
"If you prefer to hold the display awake for a longer period and you know how long you'd like to hold it, consider taking assertion kIOPMAssertionTypePreventUserIdleDisplaySleep using IOPMAssertionCreateWithDescription API instead."
Sounds closer to what you want. But I haven't tried it so I don't have a sample.

Support for up to eleven mouse buttons?

I am writing a small proof of concept for detecting extra inputs across mouses and keyboards on Windows, is it possible and how do I go about detecting input from a large amount of buttons in the Windows API? From what I have read, there is only support for 5 buttons but many mice have more buttons than that, is my question even possible with the Windows API, is it possible at all within the constraints of Windows?
You can use the Raw Input API to receive WM_INPUT messages directly from the mouse/keyboard driver. There are structure fields for the 5 standard mouse buttons (left, middle, right, x1, and x2). Beyond the standard buttons, additional buttons are handled by vendor-specific data that you would have to code for as needed. The API can give you access to the raw values, but you will have to refer to the vendor driver documentation for how to interpret them. Sometimes extra buttons are actually reported as keyboard input instead of mouse input.
Or, try using the DirectInput API to interact with DirectInput devices to receive Mouse Data and Keyboard Data.
Or, you could use the XInput API, which is the successor of DirectInput. However, XInput is more limited than DirectInput, as it is designed primarily for interacting with the Xbox 360 controller, whereas DirectInput is designed to interact with any controller. See XInput and DirectInput for more details.
Very simple: use GetKeyState
SHORT WINAPI GetKeyState(
_In_ int nVirtKey
);
Logic is next:
Ask user not to press buttons
Loop GetKeyState for all buttons 0-255
Drop pressed buttons state (some virtual keys can be pressed even it not pressed, not know why)
Now start keys monitor thread for rest keys codes and save them to any structure (pause between loop is 25ms is enough)
Ask user to press button
From keys monitor array you will see the any pressed buttons by user
Direct input and all other is more usable for other user input devices. For keyboard and mouse - GetKeyState is best.

visual basic make key always assigned [duplicate]

This question already has answers here:
VB.NET Detecting Keypress While Minimized
(3 answers)
Closed 8 years ago.
I want to assign F keys always to my program
I assigned F1 key to maximize my program
let say I open Firefox and my program minimized so when I press F1 key I want to maximize my program
What you are looking for is called a Global Keyboard Hook. Basically your program will ask the operating system to notify it whenever keys are pressed. Support for this isn't built directly in to .net, but it can be accessed using p/invoke.
There is a very good example of it, along with VB.NET source code in this stack overflow question: How to listen keyboard in background and fire keystrokes on demand?

How can I programmatically determine if an application is flashing in the taskbar [duplicate]

This question already has an answer here:
Is there a Windows API hook for "this application wants attention"?
(1 answer)
Closed 9 months ago.
I can use "FlashWindowEx" to make a window flash in the taskbar, but what can I call to determine if that has been done to a window? Is there a flag that gets set somewhere that I can query?
It's seems that such thing is not possible.
However, there may be workarounds.
For example, you may keep a boolean variable "flash = false". Then set it to "true" when you call FlashWindowEx and set to "false" in the situations in which applications typically gain focus.
References:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1846008&SiteID=1

Resources