Unity C#: How to rebind buttons via script while game is running? - unity3d

Title says it all, I'd like to implement a controls menu so the player can rebind controls, which changes the positive input key of a button in the Input Manager. But I can't find anything about it online.
How would I go about changing the positive key for a button while ingame?

Unfortunately, I believe you can't set Input Manager values via code at all. That's probably why you didn't find anything about it.
You've got two alternatives:
Use the "Input System" aka. the new input system that is available as a package. It is not as easy to use as the legacy input system, but you should check it out. It's also open source.
Use an asset store package. I've tested Rewired in the past and found it extremely powerful, especially compared to the legacy input system.
Personally, I'd first go with option 1 and see if you are happy with it. It's an official package and not 3rd party. Option 2 is still there, if you have issues with option 1, but it'll probably cost you money.

Related

Writing a macro-like tool

Recently I've been finding myself doing repetitive tasks. I would click button A, highlight text field A, type in some text, click an APPLY button, click on a drop-down box and select a specific option depending on the item I'm working with, select it and hit APPLY, then repeat this process only a couple hundred to thousand times.
So I thought maybe there's a way I could automate this? Macros then came to mind. However I've never wrote macros before so am not sure of several details such as
-what tools should I even work with?
-how do I determine which button to click?
-ideally, I would want to be able to read input from a text file to specify what should be typed in and which option should be selected from the dropdown list. Is this even possible? It seems like an operation that require some intelligence.
I am not picky on tools nor about cleanliness. I just want to be able to automate the process. It will be for personal use unless I find a convenient way such that others can use it as well.
Some details about the dropdown box: when the box is focused, I can hit the DOWN key to scroll from option to option. The items that I have to associate with these options are named exactly the same, so they appear in the same "index" order (meaning, the first item and first option appear at the top of the list, second item and second option appear after, thid item and third option...etc)
The placement of all of these fields can be fixed, so if I have to manually specify where on the screen I should be clicking, that is also a possibility.
Any idea where I should look?
If you're using Windows, AutoIt is a really nice tool.
It records actions (like a word/excel macro)
It offers a BASIC like language + API which is really easy to program (if you need to)
The API is pretty powerful
Check for Windows with a certain title
Automate klicks
...
You can "compile" your scripts into exe files so you can share your tools
It comes bundled with Scite (a nice text editor) + AutoIt syntax higlighting
But you can use any editor you want
It's well documented
It's Freeware
http://www.autoitscript.com/site/
On the Mac, there is Automator. Java has the Robot class in the basic library, to help with such automation. No doubt there are other similar tools.

PowerBuilder IDE Customization for SCC

Our PowerBuilder application is fairly large and has many objects in several PBLs for organizing our code. We often have 10 or more datawindows on one window, and these datawindows may be spread across two or three PBLs. For version control, we use exclusive check-out to avoid merge conflicts.
The situation is that when you right-click on a datawindow object from the Window painter you get a context-menu with options like "Script" and "Properties" and "Modify Datawindow...". We'd like to add one for "Check-out..." to avoid having to hunt for the datawindow in several PBLs.
Any ideas on how to do this, or something similar, would be greatly appreciated.
I think the best you can do is to create a temporary library at the top of your library list, locate your datawindows by jumping to them via "Modify Datawindow...", then saving them into your temporary library, and finally using the tools in your source control system to locate them by name and lock them.
One other trick that I use is to uncheck the tick box in the source control options that clears down the .srd etc files, then using your operating system's find tools to search on file name for these (since Powerbuilder still doesn't support searching for objects by name...). Of course if you don't have many objects, and if your objects don't have many references, you could always use Powerbuilder's search... but who do you know in the that fortunate position?!!
I think you've hit on a problem that a lot of people run into, which runs right through a loophole in PB that lets you start editing a DataWindow without warning you to check it out. Unfortunately, to the best of my knowledge there is no way to hook into the context menu.
However, you can hook into toolbar items. If that was the way I wanted to go, and I had plenty of time to spare, I'd write an app that I would launch from the toolbar, and here's what it would do:
Find the PowerBuilder window with APIs
Find the current sheet in PB
Get the object name out of the title
Get the current application (registry or PB.INI, depending on the version of PB, and may involve getting the workspace first, then the current target)
Get the library list (PB.INI or target file)
Do a LibraryExport() on the object that's open
Find all DataWindow controls (this may involve looking at ancestors to determine control types)
Identify dataobjects for these controls (again, you may need to look at ancestors)
Use LibraryDirectory() to get a list of all objects in all PBLs
Find the dataobjects' PBLs
Throw up a window listing the dataobjects and their PBLs
OTOH, if I had PBL Peeper (and, yes, this is biased advice), I'd
Launch the "PBL Peeper (Browse current application)" icon on my desktop (OK, that's a lie; I'd already have PBL Peeper open and would just switch to the Browse page)
Ctrl-Q (for QuickFind) and start typing the name of the object (if you pause, it will find a partial match on what you've typed)
Hit [Enter] once to accept QuickFind's selection
Hit [Enter] again to expand the object
Find the DataWindow control in question and RMB on it
Select "Go to Default DataWindow"
If it doesn't show the library and name in the microhelp (it's been a long time since I've released a version, and I can't keep track of what's in the released version), find the Up toolbar item to go up to the PBL
I know this doesn't achieve a checkout, but it does "avoid having to hunt for the datawindow in several PBLs". And, you can probably achieve this faster than my first suggestion.
Good luck,
Terry
The way I do it is to right-click and choose Modify DataWindow. When the painter opens you can just read the PBL from the title of the painter. Then close the DataWindow painter so PB will let you check out the DataWindow. For the more general case of locating an arbitrary user object, use Terry's PBL Peeper method.
You could separate the organization of PBLs used for development from those used for deployment.
As long as the PBL names don't conflict between the two views into the source code. The PBG files registered in source control won't clobber each other.
The downside is that when new objects are added or deleted, you will need to update both locations.
I would create a datawindow only PBL with all the related objects and put them in the same target. When I worked with that sub-system or report i could then check out all the objects in the same library.

How to highlight the differences between two versions of a text in .NET web app?

I have been supporting a web application at work for our Call Center unit for about 2 years now. The app is written in ASP.NET 3.5 with SQL server 2005 database. I’ve been asked to expand the call detail section to allow agents to edit the current call note with the ability to revert back to its previous version. Now, that’s all cool but now the manager wants to be able to click on any particular note and see all edits with changes highlighted in yellow (and if something was deleted, he wants to SEE the deleted text crossed out). Actually, what I need is very similar to how Stackoverflow handles edits on their questions. I’ve been thinking about how to go about this and after doing research and Google-ing of course, I am still unsure which route to take. I am fairly new to .NET development. Any ideas on the best technique for highlighting the changes in UI? I am afraid I am going to have to store a copy of the entire note each time they make a change because the manager wants to be able to easily review notes and revert back to ANY version (not just the most recent one) before sending the monthly call report off to our VIP customers. Since this department OFTEN changes their mind on things, I want to make sure the new functionality is scalable and easy to maintain. Any ideas would be greatly appreciated. I am really just looking for someone to point me in the right direction; maybe there are some tools out there that can be useful, recommended keywords in Google lookup, etc.
This will be difficult do to.
You'll need a "text editor" control that can not only edit the text, but which can also tell you what changes were made.
You then need to store not only the final text string, but also the list of changes
You'll then need to be able to display the text plus changes, using strike-outs, and different colors for inserts vs. changes
You'll need to do this not only for the changes of a single user, but you'll need to store each users' changes in the database, and will need to be able to display all the changes, all at once.
Your manager should be really sure he needs this.
Some tools for doing the diff for you can be found at Any decent text diff/merge engine for .NET?.
This would entail storing every version like you say. This should allow you to implement it similarly to SO. I seem to recall reading or hearing Jeff mention it, but wasn't able to find it, likely in one of the SO podcasts.
Easiest would be to store the text for each revision, then when the user wants to see the diff use a diff tool to generate the highlighted text.
Here is some Javascript diff code:
http://ejohn.org/projects/javascript-diff-algorithm/
If all the computers have Word installed you may be able to use a Word control to accomplish this. TortoiseSVN has scripts in its program directory which can take two word documents and produce a document with changes highlighted. To see this create c:\aaa.doc and bbb.doc, then install TortoiseSVN and run:
wscript.exe "C:\program files\tortoisesvn\Diff-Scripts\diff-doc.js" c:\aaa.doc c:\bbb.doc //E:javascript
I think you should see http://en.wikipedia.org/wiki/Revision_control

How can I intercept and correct keypresses at a low level?

I keep typing "t eh" instead of " the" which is, of course, annoying in the amount of time it takes me to correct myself.
The obvious answer is "Learn to type, noob!" or at least to type more slowly and/or more correctly. This error is frighteningly consistent so it appears I've trained my muscle memory for that pattern already.
But I'm wondering if it's possible to write a small, windows portable script or application that, when it detects the incorrect sequence, backspaces and corrects it automatically at a layer where it would apply to any keyboard input.
Does C# have access to that layer of the OS that intercepts keypresses systemwide?
Will I run into UAC issues with Vista?
Am I re-inventing the wheel (ie, are there open source tools I can modify or use out of the box)?
In DOS this sort of thing was quite easy and one could make TSRs (Terminate and Stay Resident) programs that would, for instance, give you a calculator onscreen with a special keypress. Not to mention the many, many practical joke programs based on this concept (dial "M" for monster!)...
I would, of course, never suggest such a utility could be used that way for co-workers...
-Adam
On windows you could use AutoHotKey. That allows you to create little scripts or macros to automate and correct things like mistypes.
One use was posted on lifehacker which took the common mistyped words and corrected them. It is at http://lifehacker.com/192506/download-of-the-day-universal-autocorrect
UPDATE Per Comment: This is Free software and windows only as far as I know.
The above script is just an example of what it can do. There are a slew of scripts available at AutoHotkeys Site
I suggest AutoHotKey. If you've never used it before, have a quick read of the tutorial: http://www.autohotkey.com/docs/Tutorial.htm
The feature you are looking for is called "hotstrings." http://www.autohotkey.com/docs/Hotstrings.htm
In your case, your script would look something like:
::teh::the
That's it! Add other things you want corrected on additional lines. AutoHotkey scripts can be compiled so you don't have to install AutoHotKey on all of your machines.
It's a very cool program. It's primary use (for making custom hotkeys) rocks! These scripts are system wide so you'll also probably want to make a hotkey to be able to turn them off too!
EDIT: In a comment, it was mentioned that he actually types "t eh" (with a space in it) and I wondered if something additional would be needed for it to work. I just tested it and it works fine. Just install autohotkey, and create a file with the .AHK extension. In that file put in the following line
::t eh::the
and save the file. Then double-click on the AHK file to load AutoHotKey with your script (you'll see a green square in your system tray to let you know it is running). It should work fine!
Yes, you can use pinvoke commands from C# to intercept the low-level os commands. I recommend you take a look at http://www.pinvoke.net. The coding isn't easy but it does work.
I suggest learning to type more slowly. I also suffer from "teh" and "ahve" in part due to autocorrect giving me the leniency. If you forced yourself to retrain then you would not be at a disadvantage when using someone else's machine.
Not to mention the unfortunate event when you need to write "t eh" and are being prevented by an overzealous 'corrector'.

Usability: Should the ENTER key close a wizard form as OK even if the focus is not set on the OK/DONE button?

I have the in my opinion odd request to close a wizard form as Done or OK if Enter was pressed on the keyboard even if the OK/DONE button is not focused.
In my opinion that would be a usability mistake. For example: In the wizard you may have multiple controls, buttons, check boxes, multiple line controls and they all have a different behavior on actions from the ENTER key. And don't forget the other buttons in the navigation of the wizard, what if they are focused?
Should these controls don't react on Enter like expected before? Should they do their actions but in other cases where Enter does no further action for the control it should close the form == inconsistent?
I think that is a typical request where the needs of one person would help him but confuse many other.
In my opinion Wizards are very special because they are not only made to make things easier but also very often focus on people with less experience with the functionality of an application. So I take every request serious and try to look into all arguments for and against the request.
Is my point of view to narrow? Are there some usability studies or guidelines especially for Wizards to backup my opinion or maybe proof me wrong?
Thank you very much!
Michael
Well, here's the thing: there are two kinds of users you have to take into account here.
First kind of users are the Baby Boomers and Gen Xers (e.g., people who have been using computers in the 70s/80s) who are accustomed to pressing Enter to move to the next field. These are the ones who learned how to use computers in terminals/consoles and enter means you're finished typing on that field and would move on to the next.
Second kind of users are those who were weaned on Windows. These people are used to pressing the Tab key to move to the next field. Pressing enter to them means they are done with the whole thing.
So which convention should you follow? That will depend on whether you're targetting the first or second type of users, the environment (Windows or Web?), and the OS.
If you're targetting Windows forms, it is much advisable to be consistent to the OS (e.g., letting people use Tab instead of Enter) for form entry. In the web, you're in a quandary, since Enter is trapped by the web browser as a submit event.
In the end the only useful advice I can offer is to try it out with your target customers and see whether they prefer Enter over Tab.
I think the key is to test. You can't really guess what your users will find comfortable, you have to watch them try it. Especially since there are multiple incompatible standards you could follow, you are just going to have to see if this change works for most users in your audience.
I would be of the same opinion, perhaps mention it to the client and let them use the final version in both modes. I guess you have to give them what they ask for when they are paying.
To me this also seems to be an odd request but as Paul says, if the client wants it, then the client gets it.
However from a usability/comprehension standpoint, I would make the border of the ok/done button much thicker then normal so that it stands out a bit and maybe indicate to people that it has special behaviour.
Also I would perhaps make a note in the dialog/wizard box that hitting enter will cause the wizard to close as if the OK/Done button had been pressed.
While the one user may know that hitting enter will close it, unless someone else is specifically told, they will not be expecting that behaviour.
I think you should have a finish page to facilitate this. If the user presses enter by mistake the worst is that he won't finish the wizard, only go to the next page (which may be the finish page). This is good for situations where nuclear bombs are controlled by said wizards.
On the finish page pressing enter would finish the wizard (and blow up Iraq, bring down a satellite, or erase Jimbob's farm).
If the user can re-run the wizard I don't think it would be disasterous if they accidentally finished it.
Remember, wizards should never take any action until they are finished, in case the user cancels or such. Confirmation dialog boxes on a finish are tedious and I will hunt you down if you use them, I think once the user has finished the wizard he is pretty sure about his intent.
Maybe the client has good reasons for it.
Imagine the following situation:
A screen with lots of optional fields that gets opened/closed a lot and where data accuracy is not really critical.
Think of a little program that pops up every half hour to ask you what you have been doing, for what client and maybe some notes so it can gather this info and generate your timesheet.
Being able to open up the screen, enter the info and close it all really quick and with as little hassle as possible is way more important than the accuracy of the data.
I can imagine lots of situations where being able to confirm the field without having focus can be usefull.
Is this request perhaps because the UAT that was undertaken on the wizard involved users that weren't aware that pressing ENTER will have the same effect as clicking the button?
If when the final page of the wizard is displayed, the 'Finish' button is already highlighted (as I would expect) that maybe it's a matter of giving the user some cue that they can also press ENTER at this point.
If you take Google for example, I seem to remember that if you tend to systematically type your search term in and then click the 'Search' button with the mouse, a message is displayed at the top of the search results that kindly hints to you that you can also just press ENTER. Obviously, this is not something that can easily be done in your case because this is the last page of the wizard, but maybe this is the sort of thing that your client is trying to get you to engineer around?
Educate your clients. Show them some documentation as why that suggestion might not be a good usability practice.
Some reputable website will work best, as clients will usually believe a third party before believing you. After all, to them you are probably just being lazy and don't want to work more.
If the client still doesn't concede, then just do what they want, and warn them that it is not the good thing to do.
Although in your case, the "good thing to do" seems a little on the gray area.
I would argue that you could possibly use this functionality to move forward through the wizard but ONLY if no other action had been taken on that page.
The moment a field is completed or a button clicked/highlighted or the cursor is moved from the default position, the Enter functionality should revert to that of the standard OS.
As others have said, clearly this would only work if those using the wizard were made aware of this as part of their application training, but it might prove useful for moving quickly through un-used pages of the wizard to get to where the user needs to be.
Doesn't matter. Choose and be consistant in all your applications