"Press any key"

Richard HaseltineRichard Haseltine Posts: 100,747

Is there a way to check if any key is pressed? There are the globals to check shift and ctrl, DzApp can check the modifiers generally and for a particular key or combo, but I don't see a method to ask if any key has been pressed (returning a string, presumably). I want to be able to stop a script with any key press, rather than having to ask for one in particular. dzApp also has the mouseEvent() signal, but there doesn't seem to be a keyboardEvent() to match

Post edited by Richard Haseltine on

Comments

  • rbtwhizrbtwhiz Posts: 2,250

    Take a look at the Background Progress sample. It doesn't show how to handle "any" key, but it does show how to handle a specific key (e.g., "Esc"); many of the other script samples show how to dynamically handle the key sequence bound to a given action. Handling "any" key would require deeper exposure of the event stack/loop than we are inclined/willing to provide.

    -Rob

  • OK, thank you, I'll do it as an specific key then.

  • MikeDMikeD Posts: 299
    edited November 2018

    Richard you can use the App.isKeyDown (number [Qt::Key value]) instead and do your work! I have modified Rob's Background Progress script and it seems to work. The problem is that we don't know the Qt::Key values of the keyboard but thankfully DAZ uses the same key codes as Microsoft. A little digging to the net and here are the codes: Keys Enum. Now what I have done to the Background Progress script:

     

    First of all:

    Declare some variables for the function:

    var bFinished = false;

    I place it under the

    // Start the progress

    comment.

    Next I set two arrays under my preview "bFinished" line, with the keys and their index numbers (Long code follows:)

    var aMapKeyNames = [		"A",		"Add",		"Alt",		"Apps",		"Attn",		"B",		"Back",		"BrowserBack",		"BrowserFavorites",		"BrowserForward",		"BrowserHome",		"BrowserRefresh",		"BrowserSearch",		"BrowserStop",		"C",		"Cancel",		"Capital",		"CapsLock",		"Clear",		"Control",		"ControlKey",		"Crsel",		"D",		"D0",		"D1",		"D2",		"D3",		"D4",		"D5",		"D6",		"D7",		"D8",		"D9",		"Decimal",		"Delete",		"Divide",		"Down",		"E",		"End",		"Enter",		"EraseEof",		"Escape",		"Execute",		"Exsel",		"F",		"F1",		"F10",		"F11",		"F12",		"F13",		"F14",		"F15",		"F16",		"F17",		"F18",		"F19",		"F2",		"F20",		"F21",		"F22",		"F23",		"F24",		"F3",		"F4",		"F5",		"F6",		"F7",		"F8",		"F9",		"FinalMode",		"G",		"H",		"HanguelMode",		"HangulMode",		"HanjaMode",		"Help",		"Home",		"I",		"IMEAccept",		"IMEAceept",		"IMEConvert",		"IMEModeChange",		"IMENonconvert",		"Insert",		"J",		"JunjaMode",		"K",		"KanaMode",		"KanjiMode",		"KeyCode",		"L",		"LaunchApplication1",		"LaunchApplication2",		"LaunchMail",		"LButton",		"LControlKey",		"Left",		"LineFeed",		"LMenu",		"LShiftKey",		"LWin",		"M",		"MButton",		"MediaNextTrack",		"MediaPlayPause",		"MediaPreviousTrack",		"MediaStop",		"Menu",		"Modifiers",		"Multiply",		"N",		"Next",		"NoName",		"None",		"NumLock",		"NumPad0",		"NumPad1",		"NumPad2",		"NumPad3",		"NumPad4",		"NumPad5",		"NumPad6",		"NumPad7",		"NumPad8",		"NumPad9",		"O",		"Oem1",		"Oem102",		"Oem2",		"Oem3",		"Oem4",		"Oem5",		"Oem6",		"Oem7",		"Oem8",		"OemBackslash",		"OemClear",		"OemCloseBrackets",		"Oemcomma",		"OemMinus",		"OemOpenBrackets",		"OemPeriod",		"OemPipe",		"Oemplus",		"OemQuestion",		"OemQuotes",		"OemSemicolon",		"Oemtilde",		"P",		"Pa1",		"Packet",		"PageDown",		"PageUp",		"Pause",		"Play",		"Print",		"PrintScreen",		"Prior",		"ProcessKey",		"Q",		"R",		"RButton",		"RControlKey",		"Return",		"Right",		"RMenu",		"RShiftKey",		"RWin",		"S",		"Scroll",		"Select",		"SelectMedia",		"Separator",		"Shift",		"ShiftKey",		"Sleep",		"Snapshot",		"Space",		"Subtract",		"T",		"Tab",		"U",		"Up",		"V",		"VolumeDown",		"VolumeMute",		"VolumeUp",		"W",		"X",		"XButton1",		"XButton2",		"Y",		"Z",		"Zoom"];	var aMapKeyIndex = [			65,			107,			262144,			93,			246,			66,			8,			166,			171,			167,			172,			168,			170,			169,			67,			3,			20,			20,			12,			131072,			17,			247,			68,			48,			49,			50,			51,			52,			53,			54,			55,			56,			57,			110,			46,			111,			40,			69,			35,			13,			249,			27,			43,			248,			70,			112,			121,			122,			123,			124,			125,			126,			127,			128,			129,			130,			113,			131,			132,			133,			134,			135,			114,			115,			116,			117,			118,			119,			120,			24,			71,			72,			21,			21,			25,			47,			36,			73,			30,			30,			28,			31,			29,			45,			74,			23,			75,			21,			25,			65535,			76,			182,			183,			180,			1,			162,			37,			10,			164,			160,			91,			77,			4,			176,			179,			177,			178,			18,			-65536,			106,			78,			34,			252,			0,			144,			96,			97,			98,			99,			100,			101,			102,			103,			104,			105,			79,			186,			226,			191,			192,			219,			220,			221,			222,			223,			226,			254,			221,			188,			189,			219,			190,			220,			187,			191,			222,			186,			192,			80,			253,			231,			34,			33,			19,			250,			42,			44,			33,			229,			81,			82,			2,			163,			13,			39,			165,			161,			92,			83,			145,			41,			181,			108,			65536,			16,			95,			44,			32,			109,			84,			9,			85,			38,			86,			174,			173,			175,			87,			88,			5,			6,			89,			90,			251];	     

    The next code goes in the if(bCancellable) bracket area of the script and replace the code of Rob:

    // If the user cancelled    			for (var j=0;j<193;j++){    				    				// If the user cancelled    				if (App.isKeyDown(aMapKeyIndex[j]) || App.isKeySequenceDown( "Esc" )){    					print(aMapKeyNames[j] + " key is pressed");    					    			//if( App.isKeySequenceDown( "Esc" ) ){	    				// Post a status update	    				App.statusLine( text( "User cancelled simple background progress test." ) );	    				// We are done...	    				bFinished = true;	    				//break;	    			}    			}    			if (bFinished) break;

    as you can see I have a loop running 193 times, as the length of my arrays.

    I put an if command with App.isKeyDown method and I kept the App.isKeySequenceDown( "Esc" ) of Rob, paired with logical "or", so if anything goes wrong the user still can end with "Esc"!

    I print out the Key pressed name (I took it from the second array). You can use the value as you want.

    I set the bFinished boolean to true and move the break command to the first loop.

    If I had Rob's permission I could paste the whole code of my modified Background Progress script here or maybe upload the script.

    Use this code and you will be amazed by the resault!

    Bye ...

    Post edited by MikeD on
  • Yeah, I thought of iterating over a list of keys, and in fact it would be OK for this application (so thank you for generating the list), but it wouldn't work as well when the process to be interrupted was more intensive and taking the time out to scan that array might well be an unacceptable overhead.

  • Eustace ScrubbEustace Scrubb Posts: 2,698
    edited November 2018

    Isn't there a way to simply test that App.isKeyDown() isn't a null set?  Or a binary (rather than index) 0?  Then the keymap in question would only matter for the N + " key is pressed" routine, if at all.

    Post edited by Eustace Scrubb on
  • App.isKeyDown() has to have a parameter, specifying a key - otherwise yes (in fact that was my thought, get the keyboard state and continue while it's empty/null).

  • rbtwhizrbtwhiz Posts: 2,250

    @Richard Keep an eye on the Change Log. 4.11.0.263 will include a change to DzApp::isKeyDown() that allows a negative number (which is -1 by default as of said build) to mean "any key". It will also include a change to DzApp::isKeySequenceDown() that allows (case-insensetive) "Any" (the default as of said build) to mean the same.

    -Rob

  • Ah, good - thank you.

Sign In or Register to comment.