/*
 * Copyright (c) 2018, Adam <Adam@sigterm.info>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.runelite.client.plugins.chatcommands;

import java.awt.event.KeyEvent;
import javax.inject.Inject;
import javax.inject.Singleton;
import net.runelite.api.Client;
import net.runelite.api.ScriptID;
import net.runelite.api.VarClientStr;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.input.KeyListener;

@Singleton
public class ChatKeyboardListener implements KeyListener
{
	@Inject
	private ChatCommandsConfig chatCommandsConfig;

	@Inject
	private Client client;

	@Inject
	private ClientThread clientThread;

	@Override
	public void keyTyped(KeyEvent e)
	{

	}

	@Override
	public void keyPressed(KeyEvent e)
	{
		if (chatCommandsConfig.clearSingleWord().matches(e))
		{
			String input = client.getVar(VarClientStr.CHATBOX_TYPED_TEXT);
			if (input != null)
			{
				// remove trailing space
				while (input.endsWith(" "))
				{
					input = input.substring(0, input.length() - 1);
				}

				// find next word
				int idx = input.lastIndexOf(' ');
				final String replacement;
				if (idx != -1)
				{
					replacement = input.substring(0, idx);
				}
				else
				{
					replacement = "";
				}

				clientThread.invoke(() ->
				{
					client.setVar(VarClientStr.CHATBOX_TYPED_TEXT, replacement);
					client.runScript(ScriptID.CHAT_PROMPT_INIT);
				});
			}
		}
		else if (chatCommandsConfig.clearChatBox().matches(e))
		{
			clientThread.invoke(() ->
			{
				client.setVar(VarClientStr.CHATBOX_TYPED_TEXT, "");
				client.runScript(ScriptID.CHAT_PROMPT_INIT);
			});
		}
	}

	@Override
	public void keyReleased(KeyEvent e)
	{

	}
}

@@ -1614,19 +1614,97 @@
 arClient
-Str
+Int;%0Aimport net.runelite.api.VarClientStr;%0Aimport net.runelite.api.vars.InputType
 ;%0Aimport
@@ -2171,70 +2171,217 @@
 %0A%09%09%09
-String input = client.getVar(VarClientStr.CHATBOX_TYPED_TEXT);
+int inputTye = client.getVar(VarClientInt.INPUT_TYPE);%0A%09%09%09String input = inputTye == InputType.NONE.getType()%0A%09%09%09%09? client.getVar(VarClientStr.CHATBOX_TYPED_TEXT)%0A%09%09%09%09: client.getVar(VarClientStr.INPUT_TEXT);%0A
 %0A%09%09%09
@@ -2774,244 +2774,346 @@
 ) -%3E
-%0A%09%09%09%09%7B%0A%09%09%09%09%09client.setVar(VarClientStr.CHATBOX_TYPED_TEXT, replacement);%0A%09%09%09%09%09client.runScript(ScriptID.CHAT_PROMPT_INIT);%0A%09%09%09%09%7D);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else if (chatCommandsConfig.clearChatBox().matches(e))%0A%09%09%7B%0A%09%09%09clientThread.invoke(() -%3E%0A%09%09%09%7B%0A%09
+ applyText(inputTye, replacement));%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else if (chatCommandsConfig.clearChatBox().matches(e))%0A%09%09%7B%0A%09%09%09int inputTye = client.getVar(VarClientInt.INPUT_TYPE);%0A%09%09%09clientThread.invoke(() -%3E applyText(inputTye, %22%22));%0A%09%09%7D%0A%09%7D%0A%0A%09private void applyText(int inputType, String replacement)%0A%09%7B%0A%09%09if (inputType == InputType.NONE.getType())%0A%09%09%7B%0A
 %09%09%09c
@@ -3162,14 +3162,22 @@
 XT, 
-%22%22
+replacement
 );%0A
-%09
 %09%09%09c
@@ -3222,18 +3222,195 @@
 NIT);%0A%09%09
-%09
 %7D
+%0A%09%09else if (inputType == InputType.PRIVATE_MESSAGE.getType())%0A%09%09%7B%0A%09%09%09client.setVar(VarClientStr.INPUT_TEXT, replacement);%0A%09%09%09client.runScript(ScriptID.CHAT_TEXT_INPUT_REBUILD, %22%22
 );%0A%09%09%7D%0A%09

support keybinds in pms