How to capture keystrokes? - erlang

I'd like to make a simple Erlang console game. I need to control my "car" in that game by key strokes - key up or W key. Is there any way to do that? I read that wx library has such capability but I'm looking for something simple and in my opinion wx library is overkill. I want to grab an event not the character from stdin.
EDIT:
Ok, I've choosen ncurses and cecho as a Erlang library for that.

I know only 2 way to do this: gs and wx.
gs is a bit simpler but the first line of the documentation is a warning telling that gs is not recommended for new application, use wx instead :o)
wx isn't so complex, but its documentation is really poor, and simply refers to wxWidget one. at least there are some demo (demo, sudoku, xrc...) that helps to start to build something. And once you succeed to have your first panels and sizers working, it is really simple to get the key stroke or mouse events an react on them. My main difficulty was in displaying graphical things fast and smooth enough even if at the end it is only a few lines of code (a few 10s).
finally, the erlang console is meant to evaluate erlang expression, so there is no chance that you can capture simple event and control the display in that environment (at a reasonable effort at least).

You can try study kernel's modules group and user_drv but they both has undocumented interface and can be subject of change in any future Erlang/OPT version. You can also made your own driver or NIF to handle termios settings and make your own implementation.
BTW I would definitely stick with wx.

Related

How to prevent command key's default OS behavior using Love2d (LÖVE) or Lua

Is there a way to prevent the default OS behavior for a command key (i.e. prevent start menu opening and focus loss when the windows key is pressed) using Love2d (LÖVE) in particular, or Lua more generally. If I understand correctly, LÖVE uses LuaJIT, so it would be helpful if a Lua based approach did not involve Lua 5.2/5.3 functions that are not implemented in LuaJIT, but I could rebuild with the appropriate module or shim the LÖVE interpreter if necessary.
Having failed to find a simple solution on my own, I'm currently thinking I will have to create a Lua-C Module. If that is the case, any advice/links along those lines would be greatly appreciated. Thanks!
Lua certainly has no functionality for hijacking OS key functionality of this kind. Such things are too OS-specific for Lua to expose. And from its documentation, Love2D has no such functionality either.
While we're on the subject:
prevent start menu opening and focus loss when the windows key is pressed
It would be exceedingly rude to have your application prevent basic OS functionality like this from working. However good your game may be, it is not as important as the user being able to do what they actually want. If the user wants to switch to something else, that's their right, and you should not interfere. If the user accidentally pressed that button, that's their responsibility.
The only thing you should do is set up a love.focus callback, so that when the application loses focus you can automatically pause.

Programming screen recorder - output issues

I want record screen (by capturing 15 screenshots per second). This part I know how to do. But I don't know how to write this to some popular video format. Best option which I found is write frames to separated PNG files and use commandline Mencoder which can convert them to many output formats. But maybe someone have another idea?
Requirements:
Must be multi-platform solutions (I'm using Free Pascal / Lazarus). Windows, Linux, MacOS
Exists some librarys for that?
Could be complex commandline application which record screen for me too, but I must have possibility to edit frames before converting whole raw data to popular video format
All materials which could give me some idea are appreciated. API, librarys, anything even in other languages than FPC (I would try rewrite it or find some equivalent)
I considered also writting frames to video RAW format and then use Mencoder (he can handle it) or other solution, but can't find any API/doc for video RAW data
Regards
Argalatyr mentioned ffmpeg already.
There are two ways that you can get that to work:
By spawning an new process. All you have to do is prepare the right input (could be a series of jpeg images for example), and the right commandline parameters. After that you just call ffmpeg.exe and wait for it to finish.
ffmpeg makes use of some dll's that do the actual work. You can use those dll's directly from within your Delphi application. It's a bit more work, because it's more low-level, but in the end it'll give you a finer control over what happens, and what you show the user while you're processing.
Here are some solutions to check out:
FFVCL Commercial. Actually looks quite good, but I was too greedy to spend money on this.
Open Source Delphi headers for FFMpeg. I've tried it, but I never managed to get it to work.
I ended up pulling the DLL wrappers from an open source karaoke program (UltraStar Deluxe). I had to remove some dependencies, but in the end it worked like a charm. The relevant (pascal) code can be found here:
http://ultrastardx.svn.sourceforge.net/viewvc/ultrastardx/trunk/src/lib/ffmpeg-0.10/
There was some earlier discussion with a Delphi component here. It's a very simple component that sometimes generates some weird movies. Maybe a start.

Is there a programming language with semantics close to English?

Most languages allow to 'tweek' to certain extend parts of the syntax (C++,C#) and/or semantics that you will be using in your code (Katahdin, lua). But I have not heard of a language that can just completely define how your code will look like. So isn't there some language which already exists that has such capabilities to override all syntax & define semantics ?
Example of what I want to do is basically from the C# code below:
foreach(Fruit fruit in Fruits)
{
if(fruit is Apple)
{
fruit.Price = fruit.Price/2;
}
}
I want do be able to to write the above code in my perfect language like this:
Check if any fruits are Macintosh apples and discount the price by 50%.
The advantages that come to my mind looking from a coder's perspective in this "imaginary" language are:
It's very clear what is going on (self descriptive) - it's plain English after all even kid would understand my program
Hides all complexities which I have to write in C#. But why should I care to learn that
if statements, arithmetic operators etc since there are already implemented
The disadvantages that I see for a coder who will maintain this program are:
Maybe you would express this program differently from me so you may not get all the
information that I've expressed in my sentence
Programs can be quite verbose and hard to debug but if possible to even proximate this type of syntax above maybe more people would start programming right? That would be amazing I think. I can go to work and just write an essay to draw a square on a winform like this:
Create a form called MyGreetingForm. Draw a square with in the middle of
MyGreetingFormwith a side of 100 points. In the middle of the square write "Hello! Click here to continue" in Arial font.
In the above code the parser must basically guess that I want to use
the unnamed square from the previous sentence, it'd be hard to write such a smart parser I guess, yet it's so simple what I want to do.
If the user clicks on square in the middle of MyGreetingForm show MyMainForm.
In the above code 'basically' the compiler must: 1)generate an event handler 2) check if there is any square in the middle of the form and if there is - 3) hide the form and show another form
It looks very hard to do but it doesn't look impossible IMO to me at least approximate this (I can personally generate a parser to perform the 3 steps above np & it's basically the same that it has to do any way when you add even in c# a.MyEvent=+handler; so I don't see a problem here) so I'm thinking maybe somebody already did something like this ? Or is there some practical burden of complexity to create such a 'essay style' programming language which I can't see ? I mean what's the worse that can happen if the parser is not that good? - your program will crash so you have to re-word it:)
Check out:
The Osmosian Order
of Plain English Programmers
Code Example:
The background is a picture.
A button has a box and a name.
To clear the status:
Clear the status' string.
Show everything.
To create the background:
Draw the screen's box with the white color.
Loop.
Pick a spot anywhere in the screen's box.
Pick a color between the lightest gray color and the white color.
Dab the color on the spot.
If a counter is past 80000, break.
If the counter is evenly divisible by 1000, refresh the screen.
Repeat.
Extract the background given the screen's box. \or Create the background from the screen. Or something.
Some Interactive fiction designers use a language syntax extremely close to the English language. Here's some Inform 7 code, which you can play online:
The foyer is a room.
The apple is in the foyer. It is edible. The description is "This is a ripe,
green granny smith apple."
The apple core is a thing. The description is "This apple core all that is
left of that granny smith apple you just consumed."
After eating the apple:
now the apple core is in the player;
say "You gobble down the apple careful not to eat any of those cyanide-
laced seeds you heard about."
I tutored a course that used Inform 7. One of the tutors had the impression the assignment was to design, not write a game. So he marked the programs by reading them, without realising they were actual programs.
I don't think that this would be an easy task nor do I think it is going to make life easier for debugging
How would you deal with these issues?
spelling mistakes
different dialects in different parts of world
different dialects in the same part of the world
synonyms
which part of sentence do you parse first?
tear (rip) and tear (from eye) both words spellings are the same but mean two different things.
Bring back COBOL or can you remember "Walk West", "Examine Door", "Push Door", "Open Door", "Use key on door" :)
edit - how would you strongly type this?
I have written an extensible English-to-Python compiler called EngScript, which converts structured English into working Python code.
This is an example of EngScript code:
print{create a string from the file called "README.txt"}
print{save the string "Woohoo!" to a file called "ExampleText.txt"}
print{the first 3 letters of "EngScript"}
This is the output that was generated by the EngScript compiler:
print(pythonFunctions.stringFromTextFile("README.txt"))
print(pythonFunctions.writeStringToFile("ExampleText.txt", "Woohoo!"))
print("EngScript"[0:(3 - 1)+1])
LiveCode!
There are a few "natural language", high-level, English-like programming or scripting languages. Probably all of them were inspired by the oldest, COBOL. My personal favorite of these languages is LiveCode. LiveCode is a decendent of MetaCard, a Linux clone of Apple’s now defunct HyperCard that used an English-like scripting language called HyperTalk, which was inspired by SmallTalk, and in turn inspired JavaScript (as well as the entire World-Wide-Web). HyperTalk was the basis for another English-Like scripting language called AppleScript (and later AppleScriptObjC), which still comes with macOS to this very day. LiveCode uses a language called LiveCodeScript, or LCS which, like other HyperCard clones and that have existed over the years (SuperCard, Adobe’s Lingo/Flash ActionScript, Open Xion, Oracle’s Toolbook, etc.), is very similar to HyperTalk at it’s core, often referred to as an X-Talk language. LiveCode has several advantages; it’s very much still in production, it has a dual license (open source and commercial versions), the engine is cross-platform (Mac, Win, Linux, HTML5, iOS, Android, and a server version), and like HyperCard it is also a GUI toolkit and it is extensible. The LiveCode team is currently working on new a lower-level programming language called LiveCode Builder, or LCB. LCB is also an English-like, although LCB is a bit less readable than LCS, it has a goals of having capabilities on par with lower-level languages like C++, Objective C, etc., allowing for extending the LiveCode platform with code libraries and frameworks produced by other programming language libraries, and ultimately allowing for the LiveCode IDE to be written in it’s own language.
Try using the programming language called 'Google' - it has a natural English interface and your code fragment throws back all the answers you are suggesting. Interestingly just six minutes after you asked this question, this very page is #1 for the query:
Check if any fruits are Macintosh
apples and discount the price by 50%
Use the Google API and I think you have the basis of a natural English programming language.

Reverse engineering and patching a DirectX game?

Background
I am playing Imperishable Night, one of the Touhou series of games. The shoot button is 'z', moving slower is 'shift', and the arrow keys move. Unfortunately for me, using shift-z ghosts my right arrow key, so I can't move to the right while shooting. This ghosting happens in all applications, and switching keyboards fixes it.
Goal
I want to locate in the disassembled code the directx function that gets the keyboard input and compares it against the 'z' key, and change that key to 'a'. I'm considering this a fun project. Assuming the size of the scan codes are the same, this should be fairly simple. And because the executable is only 400k, maybe this will provide a unique opportunity for me to explore the dark side of the computing underworld (kidding).
Relevant experience
I have some experience with coding in assembly, but not in the disassembly of such. I have no experience with the DirectX apis.
Question
I need some guidance. I've found a listing of directx keyboard scan codes, and a program called PEExplorer that looks like it will do what I need.
Is there a means by which I can turn some of the assembly with C function calls so it's more easily read? I will need to locate where the game retrieves the currently pressed keys, compares those against a list, and it's that list I need to modify.
Any input would be greatly appreciated.
You might be interested in the Detours library from Microsoft Research, it allows you to hook function calls and alter their arguments. Doing that your code can change the scan codes of keys that don't ghost into the ones the game expects.

Can I map Caps Lock to something useful for the Delphi IDE?

OK, so I know I can remap the Caps Lock key in Windows registry, but is there something useful I can map it to in the Delphi 2009 IDE? I was hoping for Code Completion (Ctrl-Space), is there a way to map the single Caps Lock key to a Ctrl-key combination?
Do yourself a favour and just download Autohotkey. This will allow you to remap the CapsLock key to absolutely any other key combination or action.
It can also do this on an application context sensitive basis (ie CapsLock automatically does different things based on the active window).
There is a useful section in the GNU Emacs FAQ For MS Windows. Emacs users like to map Caps Lock to the Ctrl key, and it should prove useful as a starting point to assigning other key combinations.
Edit: Following the trail led me to this (official) documentation page: Scan Code Mapper for Windows. Hopefully the information it contains is still relevant.
Can't you first map it some obscure non-modifier-key in the registry, then map that to the desired function in Delphi using the keyboard preferences thingy?
I personally tried the emacs-style: binding Caps Lock to the Ctrl key.
The upside is that a lot of very common actions (not just in emacs, but everywhere) can be performed with less hand moving. As just on example, deleting words backwards (which is done by Ctrl+backspace) got a whole lot easier, and it is a shortcut that works just about everywhere. For this alone, I'd map Caps Lock to the Ctrl key.
The downside here is the same as for any other non-standard modification (like Dvorak): it makes it much harder to user other people's computers. When you sit down there, you're going to definitely have a problem changing your typing instincts.
I ended up returning the Caps Lock to it usual job because I couldn't cope with not being able to type at other people's computers.

Resources