Discuss Scratch

NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

So, I'm still trying to experiment on this idea that I have on playing a Scratch project on a web page that contains a controller image map at the bottom that should register inputs that a Scratch project can recognize. I already have the graphic sets ready, I'm just trying to figure out if it's even possible. It's mainly going to be played on some iOS devices.

Anyway, I found this code snippet that someone had showed me sometime ago, but I'm unsure how, or where to place it in the html document.
document.body.dispatchEvent(new KeyboardEvent('keydown', {
key: 'a',
code: 'KeyA',
bubbles: true
}));

The idea is that if you press a defined area of the image map, it would register as a keyboard input that hopefully Scratch will detetct as in input.
Here is an example of the set up I've used some time ago.
<Table Border=2>
<TR>
<TD><Embed Src="ScratchProject.html" STYLE="width:480px; height:360px"></TD>
</TR>
<TR>
<TD><img src="controller.png" usemap="#controls" width="480" height="360"></TD>
</TR>
</Table>
<map name="controls">
<area alt="Up Arrow Key" title="Up Arrow Key" coords="85,50,146,110" shape="rect">
<area alt="Right Arrow Key" title="Right Arrow Key" coords="148,110,208,174" shape="rect">
<area alt="Left Arrow Key" title="Left Arrow Key" coords="22,110,84,174" shape="rect">
<area alt="Down Arrow Key" title="Down Arrow Key" coords="85,175,146,235" shape="rect">
<area alt="Space Bar" title="Space Bar" coords="145,285,335,325" shape="rect">
<area alt="B Button" title="B Button" coords="345,196,40" shape="circle">
<area alt="A Button" title="A Button" coords="420,125,40" shape="circle">
</map>

The issue is that I'm unsure if the script requires separate images, or if it can be done on an image map. I'm also unsure if this will even work at all, though I am willing to try anything if it is possible.

Also, at some point someone had recommended me some different ways to load the Scratch project, but I'm unsure which one to use. I tried iframe method, I tried the embed method, and I also tried the object method. In my honest opinion, they all work the same, as they all load the project anyway. Maybe there are some differences, or some browsers don't recognize one of them, I don't know. Though I would like some clarification on this.
BigNate469
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

document.body.dispatchEvent(new KeyboardEvent('keydown', {
  key: 'a',
  code: 'KeyA',
  bubbles: true
}));
There are 4 programming languages used on the web. HTML, used to build web pages. CSS, used to make HTML look pretty. JavaScript (sometimes abbreviated as JS) (not to be confused with Java, which is a different thing), used to add functionality to web pages, and make them interactive. Scratch is written in JavaScript. And finally WebAssembly, primarily used to port native applications to the web.

The important one in that list right now is JavaScript, which is what the above code is. It will fire the same event as what would happen if you pressed the “A” key on a physical keyboard- to Scratch, there would be no difference.

If you want to use JavaScript in a HTML web page, you use the <script> tag:
<script>
 //this is a JS comment. JavaScript goes here
</script>
You'll probably want to add that to the end of your <body> element, where it won't run until the rest of the body has loaded.

Last edited by BigNate469 (Aug. 30, 2024 00:31:17)


This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

So, how would I insert the JavaScript code for pushing Left, Right, Up, or Down, or A, or B from the image map defined to register as a key input to play the Scratch project?

——————————————————————————————————————————————————–

EDIT: Well, I just learned about the existence of ScratchGO…..
https://scratch.mit.edu/projects/755416315/

This is “basically” what I was aiming for as a personal goal (to have mobile controls to play Scratch projects), but as an HTML page with my own control setup. I'd STILL like to try and accomplish this so that I may have more freedom of different controllers, and to create different skins where possible.

Last edited by NMario84 (Aug. 30, 2024 04:28:53)

BigNate469
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

NMario84 wrote:

So, how would I insert the JavaScript code for pushing Left, Right, Up, or Down, or A, or B from the image map defined to register as a key input to play the Scratch project?
document.getElementById("ID attribute value of element that you want to fire the A key").addEventListener("touchstart", (event) => {
  document.body.dispatchEvent(new KeyboardEvent('keydown', {
    key: 'a',
    code: 'KeyA',
    bubbles: true
  }));
})

The above should work, although note that it's completely untested as of right now. Change things in the quotation marks as needed to get different keys.

This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

Sorry, but I am STILL confused here, and I'm not understanding where this goes in the HTML script.
So, would you place that at the very bottom of everything else, or at the top? How would I activate this code when pressing a coordinate on an image map? In other words, how would my final html code look with the image map, and the JS code together to register a keyboard input when you press a key on a coordinate of the image?

And thanks for all your help, BTW. I really appreciate it.

Last edited by NMario84 (Aug. 30, 2024 16:02:35)

BigNate469
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

NMario84 wrote:

Sorry, but I am STILL confused here, and I'm not understanding where this goes in the HTML script.
So, would you place that at the very bottom of everything else, or at the top? How would I activate this code when pressing a coordinate on an image map? In other words, how would my final html code look with the image map, and the JS code together to register a keyboard input when you press a key on a coordinate of the image?

And thanks for all your help, BTW. I really appreciate it.
That would go inside a <script> tag, it should be the last tag in your <body> tag.

The JS handles the events for you- the JS I provided above basically does
when [element 1] tapped :: hat events
fire keyboard event [a v] :: sensing stack
Replacing “element 1” (in the corresponding part of the JS) with an ID attribute value will select the element with that ID (use the id attribute in your area elements), and replace “a” in the corresponding parts of the JS to fire different keyboard events.
<script>
  document.getElementById("replace this text with the ID of the element you want to select. Equivalent of the text input in the hat block above").addEventListener("touchstart", (event) => {
    document.body.dispatchEvent(new KeyboardEvent('keydown', {
      key: 'a', //replace the "a" with whatever key you want to fire
      code: 'KeyA', //replace the "A" here with whatever key you want to fire
      bubbles: true //ignore this, and leave it alone, you won't need to touch it
    }));
  })
  //repeat the above for each HTML element you want to detect taps on
</script>
You can delete the comments afterwards, if you want.

Make sure that your <area> tags have ID attributes.

This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

So… Is “THIS” what you want me to do??

<map name="controls">
<area ID="Press B" alt="B Button" title="B Button" coords="345,196,40" shape="circle">
<area ID="Press A" alt="A Button" title="A Button" coords="420,125,40" shape="circle">
//Or does the ID="" part need to be named something else here? Does the ID need to be a number instead?
</map>

<script>
document.getElementById("Press A").addEventListener("touchstart", (event) => {
document.body.dispatchEvent(new KeyboardEvent('keydown', {
key: 'a',
code: 'KeyA',
bubbles: true
}));
})

document.getElementById("Press B").addEventListener("touchstart", (event) => {
document.body.dispatchEvent(new KeyboardEvent('keydown', {
key: 'b',
code: 'KeyB',
bubbles: true
}));
})
</script>

I get the JavaScript code here, but what I DON'T get is what the set up is here. Can the ID for the area be a description, or does it have to be a numeric value? Do I have the ID part correct in the image map?

Last edited by NMario84 (Aug. 30, 2024 20:21:17)

BigNate469
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

NMario84 wrote:

So… Is “THIS” what you want me to do??

<map name="controls">
<area ID="Press B" alt="B Button" title="B Button" coords="345,196,40" shape="circle">
<area ID="Press A" alt="A Button" title="A Button" coords="420,125,40" shape="circle">
//Or does the ID="" part need to be named something else here? Does the ID need to be a number instead?
</map>

<script>
document.getElementById("Press A").addEventListener("touchstart", (event) => {
document.body.dispatchEvent(new KeyboardEvent('keydown', {
key: 'a',
code: 'KeyA',
bubbles: true
}));
})

document.getElementById("Press B").addEventListener("touchstart", (event) => {
document.body.dispatchEvent(new KeyboardEvent('keydown', {
key: 'b',
code: 'KeyB',
bubbles: true
}));
})
</script>

I get the JavaScript code here, but what I DON'T get is what the set up is here. Can the ID for the area be a description, or does it have to be a numeric value? Do I have the ID part correct in the image map?
Just change the “ID” to lowercase (just in case), and you should be good.

This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

Okay, so what is the case for the arrow keys? Do they have any spaces for when I add them in the script?

Would it look like this?
<script>
document.getElementById("Right Arrow Key").addEventListener("touchstart", (event) => {
document.body.dispatchEvent(new KeyboardEvent('keydown', {
key: 'right arrow key',
code: 'KeyRightArrowKey',
bubbles: true
}));
})
Or this?
<script>
document.getElementById("RightArrow").addEventListener("touchstart", (event) => {
document.body.dispatchEvent(new KeyboardEvent('keydown', {
key: 'rightarrow',
code: 'KeyRightArrow',
bubbles: true
}));
})

Also, is there a web page that explains any of this, or shows what keys can be defined with this java script code? I know Scratch has limited keys available to work with, but just for the case that this mobile controller I'm working on could “in theory, hopefully” work with other projects beyond Scratch….. Right?.

BigNate469
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

NMario84 wrote:

Also, is there a web page that explains any of this, or shows what keys can be defined with this java script code? I know Scratch has limited keys available to work with, but just for the case that this mobile controller I'm working on could “in theory, hopefully” work with other projects beyond Scratch….. Right?.

For your first question, it would be “Arrow” and add the direction after that. So,
ArrowUp
ArrowDown
ArrowLeft
ArrowRight

For both values.

The first value, as I stated above, is just an ID value that you set. It could literally be “huwnudenjdeijedjfejifeijgr” and still fire a keyboard event (that isn't “huwnudenjdeijedjfejifeijgr”, which would throw an error in your browser)

For your second question, there's been one since 2005: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent (this link will take you directly to a page related to this). Note that MDN also documents HTML and CSS.

Last edited by BigNate469 (Aug. 30, 2024 22:34:22)


This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

Alright. Before I checked this topic again, I went to google for answers on javascript keyboard, and I found this that tells me about what keys I pressed.
https://www.toptal.com/developers/keycode

I will check this script out later……. Again, thanks for all your help. I really appreciate all this.
If this STILL doesn't work for me, or if I have any more issues, I'll post a response.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

Alright it's NOT working…. Or at least, I don't “think” this is working, so I've got some questions.

- When I save the HTML document, and load it up, will it work on my browser by clicking/tapping on the image map buttons with mouse button? Or must it be tried on a mobile device only?

- I noticed that when I was testing on my PC on browser with an actual keyboard, I had to “mouse click” on the game screen before my keyboard inputs would register. Is this related to the issue of tapping on the image buttons not working?

- I forgot to check for the Spacebar key. On the legend, it says for event.code it is “Space”. As for the event.key, the display reads "(blank space)". So would this be the correct input for space?
  document.getElementById("Press Space").addEventListener("touchstart", (event) => {
document.body.dispatchEvent(new KeyboardEvent('keydown', {
key: ' ',
code: 'Space',
bubbles: true
}));
})

For the information, I have copied, pasted the <script> right below the image <map> structure. But I have a feeling that it's not registering because the triggering in <area> is wrong. Are you “SURE” it is id=“” inside of <area>? Maybe perhaps it is something else, like target=“”? I ask because some of the image map generators online I've seen contain “shape”, “link”, “title”, and “target”.. So maybe it's either one of target, or title instead???

Also, if it makes any different, I am using turbowarp packager to run the scratch project as a self HTML file. So it's not directly using Scratch project ID's, from the scratch page or anything.

Last edited by NMario84 (Aug. 31, 2024 07:23:56)

BigNate469
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

1. That should work unless you're on a Mac running Safari (don't worry, it will work on iOS/iPadOS, though) or using Opera
2. This is a browser thing, nothing you can do about it. On the other hand, it makes your computer run webpages faster.
3. Yes
4. Yes, the id attribute is correct. ID is a global HTML attribute, all HTML elements support it. All it is is a unique identifier for that element, commonly used to style it using CSS or select it using JS.
5. That shouldn't matter

This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

Yeah IDK. It's NOT working. I uploaded it to a server, and tried it on mobile iOS, and nothing is happening. Maybe something is wrong, or it's not compatible? *shrugs*.

PC: Using MS Edge, running html file from my work files.
Mobile: iOS with Safari, running from website URL.

Maybe you can figure out what's wrong.
Game: http://gcnmario.free.fr/game/
Source: http://gcnmario.free.fr/game/source.html

Or maybe this was just not meant to work as such. I'd figure at LEAST I would be able to test on a browser from my PC with mouse clicks just to test touch pad conditions.
BigNate469
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

NMario84 wrote:

Yeah IDK. It's NOT working. I uploaded it to a server, and tried it on mobile iOS, and nothing is happening. Maybe something is wrong, or it's not compatible? *shrugs*.

PC: Using MS Edge, running html file from my work files.
Mobile: iOS with Safari, running from website URL.

Maybe you can figure out what's wrong.
Game: http://gcnmario.free.fr/game/
Source: http://gcnmario.free.fr/game/source.html

Or maybe this was just not meant to work as such. I'd figure at LEAST I would be able to test on a browser from my PC with mouse clicks just to test touch pad conditions.
Yeah, I just tested it on an iPad and can't get it to work either. I think that Scratch is listening for keyboard inputs at a lower level than the <body> tag…

I'll try to test it once I have access to a computer with a console that I can access.

This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

Would it matter if I were to change the embed loading to something else, like <Object> loading, or <iframe> loading instead? Maybe there is a difference on how the project is being loaded, I don't know.
BigNate469
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

NMario84 wrote:

Would it matter if I were to change the embed loading to something else, like <Object> loading, or <iframe> loading instead? Maybe there is a difference on how the project is being loaded, I don't know.
At least in the case of iframes, it would actually probably make it worse.

This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

Aright fair enough….. Well, please let me know if you find anything that will “work” for whatever this structure is. I would LOVE to be able to get this working so that I could possibly expand on the concept.

Honestly, I feel like this wouldn't be working for anything else. I mean… If the inputs from an html page are not registering for an embedded Scratch project, then what chances of it registering for some other game(s) made in some other coding environments?
BigNate469
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

NMario84 wrote:

Aright fair enough….. Well, please let me know if you find anything that will “work” for whatever this structure is. I would LOVE to be able to get this working so that I could possibly expand on the concept.

Honestly, I feel like this wouldn't be working for anything else. I mean… If the inputs from an html page are not registering for an embedded Scratch project, then what chances of it registering for some other game(s) made in some other coding environments?
I think the issue is the way Scratch registers keyboard inputs- the code I gave you should work…

This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.
NMario84
Scratcher
1000+ posts

Making a web HTML page to register inputs to a Scratch project with image map.

So, is there a way to translate the keyboard inputs into something that Scratch registers? Because whatever the case is, it is NOT working with the current set up we have.
So, we either need to find an alternative script that works for this, or find out how to make Scratch project register the keys properly.

Doing some random testing:
- I inserted one of my other scratch project ID's from an URL into the html just to see what would happen, or if there could be any differences. Nothing worked here (as usual).
- I was testing with ScratchGO's interface (Similar to what I'm trying to achieve here). It has the same issue where you can't press any of the buttons on a PC browser to move the scratch project (when I tested on MS Edge), though it does seem to work on a mobile device as intended. So, case in point I want to make here, I won't be able to test my own controls while on a PC. I'd have to test on a mobile device anyway, which is the goal.

Though I honestly think there is something missing. Whether Scratch detects/registers inputs differently or not. Maybe there is some script to manipulate the input registers of Scratch projects? IDK, maybe I am overthinking this….

Last edited by NMario84 (Aug. 31, 2024 23:33:50)

Powered by DjangoBB