Minor feature request: key press detection

  • Citizenkane
  • Topic Author
More
7 years 1 day ago - 7 years 1 day ago #1 by Citizenkane Minor feature request: key press detection was created by Citizenkane
One of my favorite features of ReShade is the ability to detect keyboard input from within a shader. It opens up so many cool possibilities for dynamic shader behavior. Currently, ReShade supports two modes of key detection: toggle, and a simple "is currently down". My suggestion is that a third mode be added: "on key press", which only resolves to true the first frame a key is pressed.

How it is currently:
uniform bool bEscapeKeyToggle <source="key"; keycode=0x1B; toggle=true;>; //ESC key

How I'm suggesting it could be:
uniform bool b1Toggle <source="key"; keycode=0x31; mode="toggle";>; //1 key
uniform bool b2Down <source="key"; keycode=0x32; mode="down";>; //2 key (true while the key is held)
uniform bool b3Press <source="key"; keycode=0x33; mode="press";>; //3 key (true only the frame the key is pressed)

Right now, I can emulate that behavior by writing booleans to a texture, but it feels super inefficient. Essentially, I am have uniforms for each key's toggle, and then check every frame to see if they've changed. Here's what I'm doing (trimmed down):
//The CheckInput method has a switch statement which uses these values
#define CHARACTER_KEY				0
#define QUESTLOG_KEY				1
#define INVENTORY_KEY				2

uniform bool bCharacterKeyToggle <source=VTMB_CHARACTER_SOURCE; keycode=VTMB_CHARACTER_KEYCODE; toggle=true;>;
uniform bool bQuestLogKeyToggle <source=VTMB_QUESTLOG_SOURCE; keycode=VTMB_QUESTLOG_KEYCODE; toggle=true;>;
uniform bool bInventoryKeyToggle <source=VTMB_INVENTORY_SOURCE; keycode=VTMB_INVENTORY_KEYCODE; toggle=true;>;

texture tStoredData { Width = 1; Height = 1; Format=RGBA8; };
sampler sStoredData { Texture = tStoredData; };
static const float2 pixel = float2(0.5, 0.5);

bool CheckInput(int key) {
	//Locations of key data in tStoredData
	//	> Pixel 0:	r=Character Sheet key		g=Quest Log key			b=Inventory key				a=UNUSED

	//If the key data from last frame is different from the key data this frame, the key was pressed!
	switch (key) {
		case CHARACTER_KEY:
			return (tex2D(sStoredData, pixel ).r != bCharacterKeyToggle);
		case QUESTLOG_KEY:
			return (tex2D(sStoredData, pixel ).g != bQuestLogKeyToggle);
		case INVENTORY_KEY:
			return (tex2D(sStoredData, pixel ).b != bInventoryKeyToggle);
	}
	return false;
}

float4 SaveDataPS(float4 pos : SV_Position, float2 uv : TEXCOORD) : SV_Target {
    return float4(bCharacterKeyToggle, bQuestLogKeyToggle, bInventoryKeyToggle, false);;
}

technique VTMB_UIMask_Bottom {	
	pass ShaderPass{
        VertexShader = PostProcessVS;
        PixelShader = MaskingPS;
    }
	
	//Write this frame's input and state data to tStoredData for use next frame
	pass SaveData{
        VertexShader = PostProcessVS;
        PixelShader = SaveDataPS;
        RenderTarget = tStoredData;
    }
}

To generate key toggle functionality in the first place, ReShade must be listening for key presses/storing key states, so it shouldn't be too difficult to extend that to include the third mode I've suggested. It's definitely the type of task the CPU is better suited for than the GPU anyways :)
Last edit: 7 years 1 day ago by Citizenkane. Reason: typo

Please Log in or Create an account to join the conversation.

  • crosire
More
7 years 1 day ago #2 by crosire Replied by crosire on topic Minor feature request: key press detection
The ReShade runtime already has checks for those events, so it's relativly easy to give shaders access the way you suggested. Good idea!
The following user(s) said Thank You: Citizenkane

Please Log in or Create an account to join the conversation.

  • Citizenkane
  • Topic Author
More
7 years 1 day ago - 7 years 1 day ago #3 by Citizenkane Replied by Citizenkane on topic Minor feature request: key press detection
Yeah, I figured ReShade was already checking those events behind the scenes. Having them available in shaders will save me a lot of branching logic, and about a hundred lines of code. :lol:

I had fun figuring out how to hack those events on my own though - I'm pretty new to writing shaders, so it was a cool exercise.

Edit: I forgot to mention it before, but it would be great to have those events for mouse buttons too!
Last edit: 7 years 1 day ago by Citizenkane.

Please Log in or Create an account to join the conversation.

  • Citizenkane
  • Topic Author
More
6 years 8 months ago #4 by Citizenkane Replied by Citizenkane on topic Minor feature request: key press detection
Hi Crosire,

I'm super happy that left mouse button input is now working properly in 3.0.8!

Any chance we'll see the feature I've requested in this thread for the next release?

Please Log in or Create an account to join the conversation.

  • crosire
More
6 years 8 months ago #5 by crosire Replied by crosire on topic Minor feature request: key press detection
Sure thing. Sorry, I forgot about this. It was added in commit 8f9e2f10656a67d78d12bd176e78e31fa19c5790 and will be available with the next release. You can also just build the source code yourself and use it now.
The following user(s) said Thank You: BlueSkyKnight, Citizenkane

Please Log in or Create an account to join the conversation.