How do I make an IF statement based on which keys are pressed in batch or powershell?

杀马特。学长 韩版系。学妹 提交于 2020-01-25 07:48:06

问题


I recently got an awesome mouse and found the back & forward buttons nearly useless. However, they were well placed so I decided to try and make a little program.

Is it possible to make a PowerShell/Batch program that will:
1. Deploy an if statement that will be triggered by a keyboard shortcut (ALT + RIGHT & ALT + LEFT)
2. Once the if statement is triggered, reject the keys as if nothing happened (most likely by sending the keys to the script instead of the current program)
3. Send new keys to do the job.

This would be like remapping the forward/back button - without any software, just a home-made solution.

FYI-

This does NOT work:
How to check if a key was pressed in Batch?


回答1:


You can do this with Powershell!.

You'd need to use a global windows hook. Looks like it would be either the WH_KEYBOARD_LL or WH_KEYBOARD hook.

To do that your PS script will need to P/Invoke several Win32 APIs and map Win32 structures to managed structures.

I've never done anything with Windows hooks, but offhand it seems like this would be moderately (if not seriously) complicated. And doing it from PS probably makes it more complicated than it would be from C/C++.

However I have hooked a WndProc from PS where I found it easier to implement the functionality I needed than in C++. In the long run it was worth the initial hurdles because a lot of functionality was implemented in PS. So it all depends on your circumstances.



来源:https://stackoverflow.com/questions/33771243/how-do-i-make-an-if-statement-based-on-which-keys-are-pressed-in-batch-or-powers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!